我正在编写一个自定义媒体选择器,并返回URI作为结果。 一切正常,除了Glide无法加载的图片。
调试后,似乎此URI是导致问题的原因,因为我使用了ContentResolver.openInputStream
并抛出了:FileNotFoundException
我的查询:
val uri = MediaStore.Files.getContentUri("external")
val projection = arrayOf(MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.TITLE,
MediaStore.Images.ImageColumns._ID,
MediaStore.Video.VideoColumns.DURATION,
MediaStore.Video.VideoColumns._ID,
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.Files.FileColumns.SIZE)
val selection = String.format("%s = %d OR %s = %d",
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE,
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO)
val sortOrder = MediaStore.Audio.Media.DATE_MODIFIED + " DESC"
val handler = MediaQueryHandler(context.contentResolver)
handler.view = WeakReference(view)
handler.startQuery(-1, null, uri, projection, selection, null, sortOrder)
获取图像文件内容的URI
ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID))))
或其他(视频文件)
ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns._ID)))
通过这种方式,在加载此URI content://media/external/images/media/10244
时出现异常
我也尝试编写一个简单的查询,例如contentResolver.query(Uri.parse("content://media/external/images/media/10244"),...)
,看起来一切正常,但仍然得到的路径是/storage/emulated/0/DCIM/tet_viet_captured/1566990942751.jpg
但是当openInputStream时,它会引发异常
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
2019-09-09 11:03:15.331 18146-18146/com.example.helloandroid.debug W/System.err: at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
2019-09-09 11:03:15.332 18146-18146/com.example.helloandroid.debug W/System.err: at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:698)
2019-09-09 11:03:15.332 18146-18146/com.example.helloandroid.debug W/System.err: at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1412)
2019-09-09 11:03:15.332 18146-18146/com.example.helloandroid.debug W/System.err: at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1249)
2019-09-09 11:03:15.333 18146-18146/com.example.helloandroid.debug W/System.err: at android.content.ContentResolver.openInputStream(ContentResolver.java:969)