android 拍照上傳照片(新)

        前段時間寫過一片關於照片上傳的文章,但是後來發現用那種方式上傳的圖片是經過android系統處理過的,並不是原圖,也就是說經過壓縮過的,圖片會變得很小,今天我就是爲了解決這個問題用另外一種方式實現。

        首先當我們要得到原有的照片必須爲拍照後的照片指定存放的路徑地址,這個地址是在Intent中指定,方法是intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

其中file就是手機本地的對應的輸出文件,這個需要在傳入參數前就生成,不然會報FileNotFoundException,下面是源碼:

                                destoryBimap();
				String state = Environment.getExternalStorageState();
				if (state.equals(Environment.MEDIA_MOUNTED)) {
					String saveDir = Environment.getExternalStorageDirectory()
							+ "/temple";
					File dir = new File(saveDir);
					if (!dir.exists()) {
						dir.mkdir();
					}
					file = new File(saveDir, "temp.jpg");
					file.delete();
					if (!file.exists()) {
						try {
							file.createNewFile();
						} catch (IOException e) {
							e.printStackTrace();
							Toast.makeText(ReportSurveyPointActivity.this,
									R.string.photo_file_isnull,
									Toast.LENGTH_LONG).show();
							return;
						}
					}
					Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
					startActivityForResult(intent, REQUEST_CODE);
				} else {
					Toast.makeText(ReportSurveyPointActivity.this,
							R.string.common_msg_nosdcard, Toast.LENGTH_LONG)
							.show();
				}

其中destoryBimap()這個方法已經在上一篇文章中介紹過了,是爲了把照片從手機內存中清楚調,不然黑容易報內存溢出異常,畢竟原圖片比較大。下面就是判斷手機是否裝載SDCard,接下來在SDCard上生成我們照片保存的地址。

        那麼接下來就是在onActivityResult()方法中加載我們的照片,這下加載照片就非常簡單了,因爲在拍照前我們已經給除了照片的路徑,我們根據這個路徑把照片加載進來就行了,下面是源碼:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_CODE) {
			if (resultCode != RESULT_OK) {
				return;
			}
			if (file != null && file.exists()) {
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 2;
				photo = BitmapFactory.decodeFile(file.getPath(), options);
				photo_image.setBackgroundDrawable(new BitmapDrawable(photo));
				pictureDir = file.getPath();
			} else {
				Toast.makeText(this, R.string.photo_file_isnull,
						Toast.LENGTH_LONG).show();
			}
		}
	}

         這裏需要說明一下的就是,當我們要在程序預覽照片的時候由於照片比較大,考慮到內存問題,所以我們要把照片進行壓縮再放到控件裏面,壓縮照片的方法就是用BitmapFactory.Options類的inSampleSize這個屬性,這個屬性的意思說在原照片的大小的多少比例來展示相片,也就是個百分比。我這裏是設置的2,也就是說原照片的一般的大小展示。

下面是銷燬照片的方法:

/**
	 * 銷燬圖片文件
	 */
	private void destoryBimap() {
		if (photo != null && !photo.isRecycled()) {
			photo.recycle();
			photo = null;
		}
	}

這個方法我是在每次拍照前和onDestroy()方法中都調用了的,都是處於內存考慮,如果你是dialog的話還建議在dimisslistener中調。

至於大家經常問道的上傳照片到後臺的方法我也順便說下,我是用的HttpClient實現的很簡單,而後臺就是隨便什麼框架都行,下面是上傳照片的方法實現:

/**
	 * 提交參數裏有文件的數據
	 * 
	 * @param url
	 *            服務器地址
	 * @param param
	 *            參數
	 * @return 服務器返回結果
	 * @throws Exception
	 */
	public static String uploadSubmit(String url, Map<String, String> param,
			File file) throws Exception {
		HttpPost post = new HttpPost(url);

		MultipartEntity entity = new MultipartEntity();
		if (param != null && !param.isEmpty()) {
			for (Map.Entry<String, String> entry : param.entrySet()) {
				if (entry.getValue() != null
						&& entry.getValue().trim().length() > 0) {
					entity.addPart(entry.getKey(),
							new StringBody(entry.getValue()));
				}
			}
		}
		// 添加文件參數
		if (file != null && file.exists()) {
			entity.addPart("file", new FileBody(file));
		}
		post.setEntity(entity);
		HttpResponse response = httpClient.execute(post);
		int stateCode = response.getStatusLine().getStatusCode();
		StringBuffer sb = new StringBuffer();
		if (stateCode == HttpStatus.SC_OK) {
			HttpEntity result = response.getEntity();
			if (result != null) {
				InputStream is = result.getContent();
				BufferedReader br = new BufferedReader(
						new InputStreamReader(is));
				String tempLine;
				while ((tempLine = br.readLine()) != null) {
					sb.append(tempLine);
				}
			}
		}
		post.abort();
		return sb.toString();
	}

 源碼下載:http://download.csdn.net/detail/yaoyeyzq/4878649

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章