Hi,
After spending whole day, I thought that I should post my experience.
I’m trying to upload image to my server using Android 1.5 SDK.
By using this method, you will be able to upload a file from your SD Card and also Bitmap image as a file.
Prerequisite: httpmime-4.1-beta1.jar
Android SDK uses apache http Client of version 3.0. That does not support for the mime type.
So download the latest HttpClient from here. (Go to latest i.e. HttpClient 4.1-Beta1, and download zip file from Binary with dependencies)
Now include the above jar file as an external jar file.
Now create a new project and update your code from following:
01 | package com.isummation.fileupload; |
03 | import java.io.BufferedReader; |
04 | import java.io.ByteArrayOutputStream; |
05 | import java.io.InputStreamReader; |
07 | import org.apache.http.HttpResponse; |
08 | import org.apache.http.client.HttpClient; |
09 | import org.apache.http.client.methods.HttpPost; |
10 | import org.apache.http.entity.mime.HttpMultipartMode; |
11 | import org.apache.http.entity.mime.MultipartEntity; |
12 | import org.apache.http.entity.mime.content.ByteArrayBody; |
13 | import org.apache.http.entity.mime.content.StringBody; |
14 | import org.apache.http.impl.client.DefaultHttpClient; |
16 | import android.app.Activity; |
17 | import android.graphics.Bitmap; |
18 | import android.graphics.Bitmap.CompressFormat; |
19 | import android.graphics.BitmapFactory; |
20 | import android.os.Bundle; |
21 | import android.util.Log; |
23 | public class FileUpload extends Activity { |
26 | /** Called when the activity is first created. */ |
28 | public void onCreate(Bundle savedInstanceState) { |
29 | super .onCreate(savedInstanceState); |
30 | setContentView(R.layout.main); |
34 | bm = BitmapFactory.decodeFile( "/sdcard/DCIM/forest.png" ); |
35 | executeMultipartPost(); |
36 | } catch (Exception e) { |
37 | Log.e(e.getClass().getName(), e.getMessage()); |
41 | public void executeMultipartPost() throws Exception { |
43 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
44 | bm.compress(CompressFormat.JPEG, 75 , bos); |
45 | byte [] data = bos.toByteArray(); |
46 | HttpClient httpClient = new DefaultHttpClient(); |
47 | HttpPost postRequest = new HttpPost( |
49 | ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg" ); |
52 | MultipartEntity reqEntity = new MultipartEntity( |
53 | HttpMultipartMode.BROWSER_COMPATIBLE); |
54 | reqEntity.addPart( "uploaded" , bab); |
55 | reqEntity.addPart( "photoCaption" , new StringBody( "sfsdfsdf" )); |
56 | postRequest.setEntity(reqEntity); |
57 | HttpResponse response = httpClient.execute(postRequest); |
58 | BufferedReader reader = new BufferedReader( new InputStreamReader( |
59 | response.getEntity().getContent(), "UTF-8" )); |
61 | StringBuilder s = new StringBuilder(); |
63 | while ((sResponse = reader.readLine()) != null ) { |
64 | s = s.append(sResponse); |
66 | System.out.println( "Response: " + s); |
67 | } catch (Exception e) { |
69 | Log.e(e.getClass().getName(), e.getMessage()); |
Modify web service URL to match yours.
Also note that I commented some lines in executeMultipartPost() method. This is used to read a file from SD card, then pass this bin object in reqEntity.addPart() method to upload any file.
FYI: png formatted file and Bitmap images are too big. So I compress it to JPEG format while uploading.
See the line:
bm.compress(CompressFormat.JPEG, 75, bos);
This is very important, because it can convert a 1.8 MB file to 180 KB!.
PS: In example, I’m using ColdFusion component (cfc) as a Restful service with JSON as return format. This is the easiest way to implement your server part. I am also using Android 1.5 SDK, because I believe that your application should be compatible with all ( varying from 1.5 to 2.3) SDK versions!.
Hope that this post helps you…
You might also like: http://vikaskanani.wordpress.com/2011/01/29/android-image-upload-activity/