Android: Upload image or file using http POST multi-part

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;
02  
03 import java.io.BufferedReader;
04 import java.io.ByteArrayOutputStream;
05 import java.io.InputStreamReader;
06  
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;
15  
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;
22  
23 public class FileUpload extends Activity {
24     Bitmap bm;
25  
26     /** Called when the activity is first created. */
27     @Override
28     public void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.main);
31         try {
32             // bm = BitmapFactory.decodeResource(getResources(),
33             // R.drawable.forest);
34             bm = BitmapFactory.decodeFile("/sdcard/DCIM/forest.png");
35             executeMultipartPost();
36         catch (Exception e) {
37             Log.e(e.getClass().getName(), e.getMessage());
38         }
39     }
40  
41     public void executeMultipartPost() throws Exception {
42         try {
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");
50             // File file= new File("/mnt/sdcard/forest.png");
51             // FileBody bin = new FileBody(file);
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"));
60             String sResponse;
61             StringBuilder s = new StringBuilder();
62  
63             while ((sResponse = reader.readLine()) != null) {
64                 s = s.append(sResponse);
65             }
66             System.out.println("Response: " + s);
67         catch (Exception e) {
68             // handle exception here
69             Log.e(e.getClass().getName(), e.getMessage());
70         }
71     }
72 }

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/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值