1. 程式人生 > >沒金幣了 簡潔的拍照和多圖上傳

沒金幣了 簡潔的拍照和多圖上傳

一、匯入 httpmine-4.3.5.jar   httpclient-4.3.5.jar


二、建立手機檔案目錄  將拍的照片路徑分成File新增到該目錄下


三、啟動系統相機 也可以自定義camera類對圖片進行處理


四、啟動子執行緒 生成客戶端傳送HttpPost請求


一個TestUpload類 佈局就為一個button按鈕

package com.test.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
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.util.EntityUtils;
import com.example.volleytest.R;
import android.app.Activity;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class TestUpload extends Activity{
	private Button bt;
	private File fileAbs;
	private ImageView image;
	private FileInputStream fis=null;
	private String mFilePath;
	private static int REQ1=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	bt=(Button) findViewById(R.id.button1);
	
	//生成路徑  ,上傳多張圖片時 可用容器儲存動態生成mFilePath路徑
	mFilePath=Environment.getExternalStorageDirectory().getPath();
	mFilePath+="/"+"newTest.png";
	//啟動系統相機
	Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	//把圖片系統預設儲存的路徑 改為我們指定的路徑
	Uri  phoneUri=Uri.fromFile(new File(mFilePath));
	intent.putExtra(MediaStore.EXTRA_OUTPUT, phoneUri);
	//根據REQ1的回撥值 可以做一些對圖片處理 ...
	startActivityForResult(intent, REQ1);
	bt.setOnClickListener(new OnClickListener() {	
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			//拍照後  啟動子執行緒上傳
			new Thread(task).start();
	
		}
	});
	
}
Runnable task=new Runnable() {	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		//建立客戶端
       HttpClient client=new DefaultHttpClient();
		String u="http://192.168.2.168:8080//goods/publish";
      //String u="http://192.168.2.168:8080/user/uploadHead";
		HttpPost post=new HttpPost(u);
		MultipartEntity muti=new MultipartEntity();
		//查詢手機本地記憶體dir目錄下的 newTest.png檔案
		File dir=Environment.getExternalStorageDirectory();
		fileAbs=new File(dir,"newTest.png");		
		FileBody fileBody=new FileBody(fileAbs);
		
		//muti.addPart("param1", new StringBody("中國", Charset.forName("UTF-8")));
		try {
			//新增ID欄位值  第一個引數為介面欄位 第二個為其值
			muti.addPart("userId", new StringBody("40288228576f329501576fae2a81000e"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//新增file 第一個引數為介面欄位 服務端為files[]容器    第二個為其值
		muti.addPart("files", fileBody);
		muti.addPart("files", fileBody);
		post.setEntity(muti);		
		try {
			//獲取服務端返回結果 並列印
			HttpResponse response=client.execute(post);
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				System.out.println(EntityUtils.toString(response.getEntity())+"上傳成功了");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
};
}