MainActivity如下:
package cc.cn; import java.io.File; import java.io.IOException; import cc.cn.UploadImageThread.UploadImageListener; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.Button; /** * Demo描述: * 利于httpmime-4.1.3.jar实现图片的上传 */ public class MainActivity extends Activity { private Button mCameraButton; private File imageFile; private Context mContext; private UploadImageThread mUploadImageThread; private final int REQUEST_CAMERA = 9527; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { mContext=this; mCameraButton = (Button) findViewById(R.id.button); mCameraButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startCamera(); } }); } public void startCamera() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { String saveDir = Environment.getExternalStorageDirectory()+ "/test"; File imageDir = new File(saveDir); if (!imageDir.exists()) { imageDir.mkdir(); } imageFile = new File(saveDir, "floor1.jpg"); if (!imageFile.exists()) { try { imageFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); return; } } Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); startActivityForResult(intent, REQUEST_CAMERA); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_CAMERA: if (resultCode != RESULT_OK) { return; } if (imageFile != null && imageFile.exists()) { mUploadImageThread= new UploadImageThread(imageFile); mUploadImageThread.setUploadImageListener(new UploadImageListener(){ @Override public void uploadImageFail() { System.out.println("----> 图片上传失败"); } @Override public void uploadImageSuccess() { System.out.println("----> 图片上传成功"); }}); mUploadImageThread.start(); } break; default: break; } } }
UploadImageThread如下:
package cc.cn; import java.io.File; import java.nio.charset.Charset; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.ContentBody; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.CoreProtocolPNames; public class UploadImageThread extends Thread { private File mImageFile; private String serverURL = "Your serverURL"; private UploadImageListener mUploadImageListener; public UploadImageThread(File imageFile) { mImageFile = imageFile; } public void setUploadImageListener(UploadImageListener uploadImageListener){ mUploadImageListener=uploadImageListener; } public interface UploadImageListener{ public void uploadImageFail(); public void uploadImageSuccess(); } @Override public void run() { HttpPost httppost = new HttpPost(serverURL); ContentBody contentBody1 = new FileBody(mImageFile, ".jpg"); HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter (CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); httpclient.getParams().setParameter (CoreConnectionPNames.CONNECTION_TIMEOUT, 15000); httpclient.getParams().setParameter (CoreConnectionPNames.SO_TIMEOUT,20000); MultipartEntity entity = new MultipartEntity (HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8")); try { entity.addPart("devsn", new StringBody("123456")); entity.addPart("status", new StringBody("123456")); entity.addPart("upfile1", contentBody1); httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); System.out.println("---> statusCode="+status); if (status == HttpStatus.SC_OK) { mUploadImageListener.uploadImageSuccess(); } else { mUploadImageListener.uploadImageFail(); } } catch (Exception e) { mUploadImageListener.uploadImageFail(); } finally { httpclient.getConnectionManager().shutdown(); } } }
main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照并且上传到服务器" android:layout_centerInParent="true" /> </RelativeLayout>