hello i am new in android devlopment i want to know how to upload an image in android i dont found any usefull tutorial for this can u give me some instruction,pls,help me out.
-
Can you be more specific what you want to do with this image? There are many ways to work with images, and I need to choose the one most suitable for your needs. With best, – Patrick Sep 27 '10 at 06:28
-
All that you want is [here](http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/). – Vikas Jan 29 '11 at 09:49
2 Answers
I built this lil methods for you:
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;
}
Should work so far. On your webserver you need some PHP Script which returns a "1" for a successful upload and something else for an error. I also suggest to do this in a ASyncTask, to prevent blocking the user during the uploading. On the webserver side you've got a file in the name "uploadedfile". Hope that helps!

- 1,647
- 2
- 18
- 34
-
Would you update your answer after reading this answer? http://stackoverflow.com/a/2926550/8418 (I was trying to upload to App Engine and after that fix.. it worked!) – Lipis Jun 07 '12 at 01:43
-
Hi, I was trying to use this code today, but the lines `outStream = new DataOutputStream(connection.getOutputStream());` and `inStream = new DataInputStream( connection.getInputStream() );` are throwing IOExceptions. I have no idea why! can you help? – Richard Jun 18 '12 at 16:49
-
What kind of IO Exception is it? Where do you use the code? Emulator or Phone, with an SD Card or not? Does the Connection work? – Keenora Fluffball Jun 20 '12 at 07:09
-
I don't have a tutorial bout it. Here you have an example: np.
POST / HTTP/1.1
Host: jmaster
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: pl,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
Referer: http://shop/index.php/index/register/b/
Content-Type: multipart/form-data; boundary=---------------------------19187836022413
X-Forwarded-For: 127.0.0.1
X-Forwarded-Host: jmaster
X-Forwarded-Server: jmaster
Connection: Keep-Alive
Content-Length: 38682
-----------------------------19187836022413
Content-Disposition: form-data; name="file2"; filename="Clipboard02.png"
Content-Type: image/png
‰PNG
?
... and this is how it goes.
-----------------------------19187836022413
and you're ending transmission.
----------------------------19187836022413
hope this helps.

- 472
- 3
- 16