Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android拍照且將圖片上傳至服務器

Android拍照且將圖片上傳至服務器

編輯:關於Android編程

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如下:




    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved