【问题标题】:Weird behaviour while trying to get picture from DCIM尝试从 DCIM 获取图片时的奇怪行为
【发布时间】:2014-12-18 17:41:08
【问题描述】:

我正在尝试从 DCIM 加载我的活动中的图片。 我使用以下代码:

int BROWSE_PICTURES = 0;
public void openBrowsePictures() {
    Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, BROWSE_PICTURES);
}

在 onActivityResult 中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == BROWSE_PICTURES && resultCode == RESULT_OK && null != data) { // we have bitmap from filesystem!
        Uri selectedImage = data.getData();
        Log.d("CAMERA","____"+selectedImage.toString());


        String[] filePathColumn = {MediaStore.Images.Media.DATA};



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

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        Log.d("CAMERA", " column : " + columnIndex);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);


        Log.d("CAMERA", "----" + filePath);
    }
}

当我尝试从文件系统加载拍摄的图片时,情况变得很奇怪。有用 正如预期的那样。我从 EasyScreenshot 文件中选择了一张图片,但是当 b.我从 DCIM/Camera 路径中选择图片它不起作用。 如果我在 a 处运行 Log.d 代码。案例打印:

相机:____content://media/external/images/media/27487

和第二个 Log.d :

相机:----/storage/emulated/0/Pictures/Screenshots/Screenshot_2014-12-18-15-14-22.png

但是,如果 b 它打印以下内容:

第一个 log.d :

摄像头:____content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2F7tUacBA_4oYS2Q8CmkINWHa93B_n7heNyt3OyVZgkY8%3Ds0-d

和第二个 log.d :

相机:----null

我在运行 Android 5.0.1 的 nexus 4 设备上测试应用程序

提前谢谢你

【问题讨论】:

    标签: java android android-intent picturegallery


    【解决方案1】:

    您不能假设媒体选择器返回的 Uri 将对应于本地文件。您似乎在选择 G+ 照片或设备中没有的其他图片。

    正确的方法是使用ContentResolver 以流的形式访问图片。例如:

    InputStream inputStream = null;
    if (ContentResolver.SCHEME_CONTENT.equals(selectedImage.getScheme())) {
        inputStream = context.getContentResolver().openInputStream(selectedImage);
    } else if (ContentResolver.SCHEME_FILE.equals(selectedImage.getScheme())) {
        inputStream = new FileInputStream(selectedImage.getPath());
    }
    
    bitmap = BitmapFactory.decodeStream(inputStream);
    

    这应该适用于 content://file:// uris。

    并且(非常重要)确保从后台线程(例如 AsyncTask)执行此操作,否则如果 uri 是“远程”的,您将获得 NetworkOnMainThreadException

    【讨论】:

    • 您使用的“selectedImage”和“mediaUri”这两个变量有什么区别?谢谢。
    • @issathink 实际上我编辑了我的代码以匹配 OP 的变量名称,但错过了一个用法。感谢您指出。
    • 不敢相信文档或示例中的任何地方都没有指出这一点......
    【解决方案2】:

    您必须使用内容提供商处理 content://com.google.android.apps.photos.content 类型的 URL,就像您已经做到的那样。但是,您应该尝试这样做,跳过真实的文件路径,只获取位图本身。

    https://stackoverflow.com/a/19835603/4380308

    【讨论】:

    • 我切换到你提到的 INTERNAL_CONTENT_URI 但它的行为完全相同..
    猜你喜欢
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多