【问题标题】:Android get full size image from cameraAndroid从相机获取全尺寸图像
【发布时间】:2013-12-03 11:55:13
【问题描述】:

我正在开发一个 Android 应用程序,该应用程序将图像从相机或设备照片库上传到远程站点。后者我工作正常,我可以选择和上传。但是,我无法获取全尺寸图像并上传。这是我的代码:

// From onCreate
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

我有一个方法来处理 Activity 结果。这两个都处理从图库和相机中进行选择:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

  String filepath = ""; 
  Uri selectedImageUri;

  if (resultCode == RESULT_OK) {
    if (requestCode == CAMERA_PIC_REQUEST) {
      Bitmap photo = (Bitmap) data.getExtras().get("data");
      // Gets real path of image so it can be uploaded
      selectedImageUri = getImageUri(getApplicationContext(), photo);
    }
    else {
      selectedImageUri = data.getData();
    }
    // Handle the upload ...
  }
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}

这可行,但是图像很小并且不遵循存储图像的命名约定。我已经读过,要保存完整尺寸,我需要使用 putExtra 并在 cameraIntent 上传递 MediaStore.EXTRA_OUTPUT 并声明一个临时文件,因此我将我的意图代码调整如下:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
camImgFilename = sdf.format(new Date())+".jpg";

File photo = new File Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), camImgFilename);

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

但是,这会导致引发错误,指出“找不到源”。

不知道从哪里开始?

更新

似乎正在创建我的照片。应用程序关闭后,我可以使用文件资源管理器导航到目录并查看图像。我不确定是什么导致应用崩溃。

【问题讨论】:

标签: android image image-processing bitmap android-camera-intent


【解决方案1】:

我认为问题在于您仍在尝试从Intent 访问缩略图。为了检索完整尺寸的图像,您需要直接访问该文件。所以我建议在开始相机活动之前保存文件名,然后在onActivityResult加载文件。

我建议阅读Taking Photos Simply page from the official documentation,您可以在其中找到有关缩略图的引用:

注意:这个来自“数据”的缩略图可能适合作为图标,但不是很多。处理全尺寸图像需要更多的工作。

同样在最后一节中,您会找到所需的代码:

private void setPic() {
    // Get the dimensions of the View
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    mImageView.setImageBitmap(bitmap);
}

【讨论】:

  • 谢谢你的回答,去掉Bitmap photo = (Bitmap) data.getExtras().get("data");已阻止我的应用程序崩溃,并且上面列出的 galleryAddPic() 方法 setPic() 列出了图库中的图像。完美!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 2011-09-20
  • 1970-01-01
相关资源
最近更新 更多