【问题标题】:Cannot fetch images taken by camera from Gallery无法从图库中获取相机拍摄的图像
【发布时间】:2014-12-23 04:27:52
【问题描述】:

我正在尝试使用以下代码从图库中获取图像:

private void initGalleryFetchImage() {
    Intent i = new Intent(
      Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getActivity().getContentResolver().query(selectedImage,
            filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        LOGI(TAG, "picturePath: " + picturePath);
        if (picturePath.startsWith("http")) {
            Picasso.with(getActivity()).load(picturePath).into(iv);
        } else {
            Picasso.with(getActivity()).load(new File(picturePath)).into(iv);
        }
    }
}

我可以检索除已拍摄的图像之外的所有图像 并由相机应用程序存储。

[☓] picturePath: /storage/emulated/0/DCIM/Camera/20141223_102304.jpg

[✓] picturePath: https://lh4.googleusercontent.com/...f3rf3.jpg
[✓] picturePath: /storage/emulated/0/DCIM/Facebook/IMG_13280625132331.jpeg
[✓] picturePath: /storage/emulated/0/Pictures/Instagram/IMG_20140521_232443.jpg
[✓] picturePath: /storage/emulated/0/LINEcamera/2014-04-12-19-47-52_deco.jpg
[✓] picturePath: /storage/emulated/0/foursquare/foursquare/1403867797152.jpg

有人知道如何解决这个问题吗?

【问题讨论】:

  • 你在androidManifest.xml设置权限了吗?
  • 你如何存储捕获的图像??
  • 我已正确设置权限。我刚刚从相机应用程序中拍了一张照片,并尝试从图库图像选择器中选择它

标签: android image android-camera android-gallery picasso


【解决方案1】:
try this


private void initGalleryFetchImage() {
    Intent i = new Intent(
      Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK ) {
Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                try {
                    bmp = BitmapFactory.decodeStream(getContentResolver()
                            .openInputStream(selectedImage));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
}}

【讨论】:

  • 这似乎现在可以获取图像,但它也在奇怪地旋转它!
  • @rishabhmhjn 你检查过我的答案了吗?
  • @kaushik 回复了你的回答
【解决方案2】:

onActivityResult(...) 中使用 selectedImage 这是一个 Uri 用于加载图像

if (picturePath.startsWith("http")) {
      Picasso.with(getActivity()).load(picturePath).into(iv);
} else {
      Picasso.with(getActivity()).load(selectedImage).into(iv);
}

【讨论】:

  • 感谢您的建议。我删除了http检查并将URI直接应用于毕加索,但问题仍然存在
  • 请检查else 部分是否已执行。
  • 是的,我将那部分保留在代码中,因为第一个不再需要了
猜你喜欢
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多