【发布时间】:2010-09-27 06:23:01
【问题描述】:
你好,我是 android devlopment 的新手 我想知道如何在android中上传图片 我没有找到任何有用的教程 你能给我一些指导吗,请帮帮我。
【问题讨论】:
-
你能更具体地说明你想用这张图片做什么吗?处理图像的方法有很多种,我需要选择最适合您需求的一种。最好的,
-
你想要的只是here。
标签: android
你好,我是 android devlopment 的新手 我想知道如何在android中上传图片 我没有找到任何有用的教程 你能给我一些指导吗,请帮帮我。
【问题讨论】:
标签: android
我为你构建了这个 lil 方法:
private boolean handlePicture(String filePath, String mimeType) {
HttpURLConnection connection = null;
DataOutputStream outStream = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "http://www.yourwebserver.com/youruploadscript.php";
try {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(new File(filePath));
} catch(FileNotFoundException e) { }
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outStream = new DataOutputStream(connection.getOutputStream());
outStream.writeBytes(addParam("someparam", "content of some param", twoHyphens, boundary, lineEnd));
outStream.writeBytes(twoHyphens + boundary + lineEnd);
outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + filePath +"\"" + lineEnd + "Content-Type: " + mimeType + lineEnd + "Content-Transfer-Encoding: binary" + lineEnd);
outStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outStream.writeBytes(lineEnd);
outStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
outStream.flush();
outStream.close();
} catch (MalformedURLException e) {
Log.e("DEBUG", "[MalformedURLException while sending a picture]");
} catch (IOException e) {
Log.e("DEBUG", "[IOException while sending a picture]");
}
try {
inStream = new DataInputStream( connection.getInputStream() );
String str;
while (( str = inStream.readLine()) != null) {
if(str=="1") {
return true;
} else {
return false;
}
}
inStream.close();
} catch (IOException e){
Log.e("DEBUG", "[IOException while sending a picture and receiving the response]");
}
return false;
}
private String addParam(String key, String value, String twoHyphens, String boundary, String lineEnd) {
return twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd + lineEnd + value + lineEnd;
}
到目前为止应该可以工作。在您的网络服务器上,您需要一些 PHP 脚本,该脚本返回“1”表示成功上传,其他内容表示错误。我还建议在 ASyncTask 中执行此操作,以防止在上传期间阻止用户。 在网络服务器端,您有一个名为“uploadedfile”的文件。希望对您有所帮助!
【讨论】:
outStream = new DataOutputStream(connection.getOutputStream()); 和 inStream = new DataInputStream( connection.getInputStream() ); 行正在抛出 IOExceptions。我不知道为什么!你能帮忙吗?
我没有关于它的教程。这里有一个例子:np.
POST / HTTP/1.1
主持人:jmaster
用户代理:Mozilla/5.0(Windows;U;Windows NT 5.1;pl;rv:1.9.2.10)Gecko/20100914 Firefox/3.6.10
接受:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
接受语言:pl,en-us;q=0.7,en;q=0.3
接受编码:gzip,deflate
接受字符集:ISO-8859-2,utf-8;q=0.7,*;q=0.7
推荐人:http://shop/index.php/index/register/b/
内容类型:multipart/form-data;边界=---------------19187836022413
X-Forwarded-For:127.0.0.1
X-Forwarded-Host: jmaster
X-Forwarded-Server: jmaster
连接:保持活动
内容长度:38682
-----------------------------19187836022413
内容处置:表单数据;名称=“文件2”;文件名="剪贴板02.png"
内容类型:image/png
‰PNG
?
...事情就是这样。
-----------------------------19187836022413
你正在结束传输。
--------------19187836022413
希望这会有所帮助。
【讨论】: