Android 調用本地相機拍照並上傳圖片

1、調用本地相機拍照並上傳圖片
			Intent cameraIntent = new Intent(
					"android.media.action.IMAGE_CAPTURE");
			startActivityForResult(cameraIntent, mPictureRequestCode);
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == mPictureRequestCode) {
			if (resultCode == RESULT_OK) {
				Uri uri = data.getData();
				if (uri == null) {// 如果爲空
				} else {
					Log.e("uri", uri.toString());
					ContentResolver cr = this.getContentResolver();
					try {
						// 得到bitmap圖像
						// Bitmap bitmap = BitmapFactory.decodeStream(cr
						// .openInputStream(uri));

						// 獲取圖片路徑
						String[] proj = { MediaStore.Images.Media.DATA };
						// 多媒體數據庫的封裝接口
						Cursor cursor = managedQuery(uri, proj, null, null,
								null);

						// 獲得用戶選擇的圖片的索引值
						int column_index = cursor
								.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
						// 將光標移至開頭 ,避免引起越界
						cursor.moveToFirst();
						// 獲取圖片路徑
						filepath = cursor.getString(column_index);
						Log.e("uri", filepath);
					} catch (Exception e) {
						Log.e("Exception", e.getMessage(), e);
					}

				}
			}
		}
	}

	/**
	 * 上傳文件(圖片)
	 */
	private void uploadFile() {
		if (null == filepath || !filepath.contains("/"))
			return;
		netCode = CommonUtil.isNetworkAvailable(getApplicationContext());
		if (netCode == 0) {
			Toast.makeText(getApplicationContext(), "請檢查網絡連接", 0).show();
		} else {
			// long time = System.currentTimeMillis();
			// String date = MyDateUtils.formatDateAndTime(time);
			try {
				String uploadHost = "http://"
						+ PersonInfomation.server_ip_upload
						+ ":8080/cjyw/UploadAndroidImp";
				RequestParams params = new RequestParams();
				params.addBodyParameter(filepath.replace("/", ""), new File(
						filepath));
				uploadMethod(params, uploadHost, 1);
			} catch (Exception e) {
				// TODO: handle exception
			}

		}
	}


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