0

I'm writing a custom media picker and return URI to result. Everything is Ok except an image that Glide couldn't load.

After debug, seem like this URI is the problem cause I use ContentResolver.openInputStream and it throw :FileNotFoundException

My query :

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)

Get URI of content of Image file

ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID))))

Or other(video file)

ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns._ID)))

By this way, I've exception while load this URI content://media/external/images/media/10244
I also try write a simple query like contentResolver.query(Uri.parse("content://media/external/images/media/10244"),...) and seem like everything is OK, also still got path is /storage/emulated/0/DCIM/tet_viet_captured/1566990942751.jpg

But when openInputStream, it throws exception

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)
Kabir
  • 852
  • 7
  • 11
tqn
  • 125
  • 12
  • [Please refer to this link below. You may find solution](https://stackoverflow.com/questions/13549559/getcontentresolver-openinputstreamuri-throws-filenotfoundexception) – Govind Sharma Sep 09 '19 at 07:01
  • @GovindSharma Thanks. It seem like a different case. In this case, I wrote a picker myself and query all media by my URI, which set to external. – tqn Sep 09 '19 at 07:17

0 Answers0