简体   繁体   English

获取 Gallery Intent 选择的图像路径时出错(Android 6 - 某些设备)

[英]Error getting image path selected by Gallery Intent (Android 6 - Some devices)

I am trying to get the path of an image when the user pick from the gallery (with intent)当用户从图库中选择时,我试图获取图像的路径(有意)

It's been working ok, since some users noticed that could not do it witn Android 6.0.它一直工作正常,因为一些用户注意到在 Android 6.0 上无法做到。

I have tried different things, and some solutions works in the emulator with Android 6.0 but not in my Xiamoi with Android 6.1.我尝试了不同的方法,一些解决方案在 Android 6.0 的模拟器中有效,但在我的 Android 6.1 虾米中无效。

This both solutions works in the emulator (6.0) and Android 4.4 physycal device.这两种解决方案都适用于模拟器 (6.0) 和 Android 4.4 物理设备。

public String getRealPathFromURI(Activity context, Uri contentURI) {
    String[] projection = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = context.managedQuery(contentURI, projection, null,
            null, null);
    if (cursor == null)
        return null;
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    if (cursor.moveToFirst()) {
        String s = cursor.getString(column_index);
        // cursor.close();
        return s;
    }
    // cursor.close();
    return null;
}

and the other similar:另一个类似的:

private static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

But in my Xiaomi 6.1 the cursor is null.但是在我的小米 6.1 中,光标为空。 But I can get the real path from:但我可以从以下位置获得真正的路径:

private static String getRealPathFromURI(Context context, Uri contentUri) {
    return contentUri.getEncodedPath();
}

Any help?有什么帮助吗? Thank you!谢谢!

EDIT:编辑:

I'm asking for choose an image in this way:我要求以这种方式选择图像:

Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK); //ACTION_GET_CONTENT
// Always show the chooser (if there are multiple options available)
launchForResult(Intent.createChooser(intent, "Select Picture"), SELECT_FILE);

I'm asking for choose an image in this way:我要求以这种方式选择图像:

First, use ACTION_GET_CONTENT to pick by MIME type.首先,使用ACTION_GET_CONTENT按 MIME 类型进行选择。

Second, whatever activity that responds to ACTION_GET_CONTENT (or ACTION_PICK ) does not need to return a Uri that the MediaStore knows about.其次,任何响应ACTION_GET_CONTENT (或ACTION_PICK )的活动都不需要返回MediaStore知道的Uri In fact, most will not.事实上,大多数不会。 They can return a Uri that points to anything , including things that are not files.他们可以返回一个指向任何东西Uri ,包括不是文件的东西。

So, get rid of all of your broken getRealPathFromURI() code.因此,摆脱所有损坏的getRealPathFromURI()代码。 Use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri , and use that stream.使用ContentResolveropenInputStream()Uri标识的内容上获取InputStream ,并使用该流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM