Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中使用URI中的ParcelFileDescriptor进行图像选择_Android_Image_Android Intent_Uri - Fatal编程技术网

在Android中使用URI中的ParcelFileDescriptor进行图像选择

在Android中使用URI中的ParcelFileDescriptor进行图像选择,android,image,android-intent,uri,Android,Image,Android Intent,Uri,我的应用程序正在使用ACTION\u GET\u Intent创建意图,以查找图像mime类型。返回后,我完成了一个漫长而复杂的过程,包括检查数据的意图、从URI的内容解析器获取游标、检查URI.toString()以及处理某些URI与其他URI不同的内容。例如“content://com.google.android.gallery3d“将是”content://com.google.android.apps.docs.storage“等等 这是一件痛苦的事情,尤其是KitKat出现并引入了更

我的应用程序正在使用ACTION\u GET\u Intent创建意图,以查找图像mime类型。返回后,我完成了一个漫长而复杂的过程,包括检查数据的意图、从URI的内容解析器获取游标、检查URI.toString()以及处理某些URI与其他URI不同的内容。例如“content://com.google.android.gallery3d“将是”content://com.google.android.apps.docs.storage“等等

这是一件痛苦的事情,尤其是KitKat出现并引入了更多的URI来处理

但后来我在一些与SAF和KitKat相关的Google开发者页面上找到了这段建议代码。这似乎很有效。我在KitKat、JB和GB设备上测试了所有已安装的“文件选择器”类应用程序。它从未失败过

**所以我的问题是……这个代码可靠吗?还有什么需要注意的吗? 如果这是首选方法,为什么会有那么多关于如何处理从选择图像返回的Intent/URI的帖子

代码示例位于: 查看“位图”部分

try {
        final ParcelFileDescriptor parcelFileDescriptor = myContext.getContentResolver().openFileDescriptor(
                imageUri, "r");
        final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return bitmap;
    } catch (Exception e) {
        Log.e(tag, "Failed to Parse Image Uri", e);
        throw new Exception("failed to parse image uri");
    }

您上面提到的代码是所有设备的完美解决方案。因为我们有一个自动备份文件夹和一些特定的设备uri问题。因此,如果您使用FileDescriptor,它将在所有设备中正常工作。 我还使用了相同的代码,并在11台设备上进行了测试,如2.3.3版到4.4.2版。 上述代码的优点是: 我们可以从设备库包括自动备份文件夹中选择任何图像。但您提到的现有代码不适用于从gallery中选择视频(仅适用于自动备份文件夹)