关闭

Android 调用本地相机拍照并上传图片

标签: Android图片上传
475人阅读 评论(0) 收藏 举报
分类:
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
			}

		}
	}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15178次
    • 积分:585
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:3篇
    • 译文:0篇
    • 评论:1条
    最新评论