Crashing When selecting video in android Q

Viewed 848

My Below code working fine rest of the android versions but crashing in android Q when I tried to select video

Added write and read permission My SDK Version is 28.0.2 Video recording is working fine for above code

Using this below code to start intent

    val videoIntent = Intent(Intent.ACTION_PICK) 
    videoIntent.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*") 
    videoIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) 
    videoIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true) 
    videoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) startActivityForResult(videoIntent, 
    GALLERY_VIDEO)

In OnActivityResult

if (data?.data != null) {
                if (data.clipData != null) {
                    val mClipData = data.clipData
                    for (i in 0 until mClipData!!.itemCount) {
                        val item = mClipData.getItemAt(i)
                        val uri = item.uri
                        val videoPath = fileUtil.getVideoPath(this, uri)
                        if (videoPath != null) {
                            videoList.add(VideoType(videoPath, videoPath))
                        }
                        videoPath?.let { MediaType(it, videoPath, VIDEO) }?.let { mediaUrls.add(it) }
                        videoListAdapter.notifyDataSetChanged()
                    }
                } else {
                    val contentURI = data.data
                    val videoPath = fileUtil.getVideoPath(this, contentURI)
                    val type = videoPath?.let { MediaType(it, contentURI.toString(), VIDEO) }
                    if (videoPath != null) {
                        videoList.add(VideoType(videoPath, videoPath))
                    }
                    videoPath?.let { MediaType(it, videoPath, VIDEO) }?.let { mediaUrls.add(it) }
                    videoListAdapter.notifyDataSetChanged()
                }
            }

getVideoPath method

fun getVideoPath(context: Context, uri: Uri?): String? {
    val projection = arrayOf(MediaStore.Video.Media.DATA)
    var contentUri = ""
    val cursor =
            context.contentResolver.query(uri!!, projection,
                    null, null, null)
    if (cursor != null) {
        val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
        cursor.moveToFirst()
        contentUri = cursor.getString(columnIndex)
        cursor.close()
    }
    return contentUri
}

Log message

  java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=101, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/2/content://media/external/video/media/26/ORIGINAL/NONE/529191973 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F26/ORIGINAL/NONE/529191973} }} to activity {com.rizek.android.users/com.rizek.android.users.ui.mediaupload.MediaUploadActivity}: java.lang.IllegalArgumentException: Invalid column latitude
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4845)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4886)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.IllegalArgumentException: Invalid column latitude
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:423)
    at android.content.ContentResolver.query(ContentResolver.java:944)
    at android.content.ContentResolver.query(ContentResolver.java:880)
    at android.content.ContentResolver.query(ContentResolver.java:836)
    at com.rizek.android.users.utils.file.FileUtil.getVideoPath(FileUtil.kt:41)
    at com.rizek.android.users.ui.mediaupload.MediaUploadActivity.onActivityResult(MediaUploadActivity.kt:244)
1 Answers

Had the same issue, managed to get it to work by Changing the Intent from ACTION_PICK, and adding extra flags, like:

        Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        i.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*");
        ((AppCompatActivity) context).startActivityForResult(i, Utils.VIDEO_GALLERY_REQUEST_CODE);

then in onActivityResult() getting a persistable permission.

        if (requestCode == Utils.VIDEO_GALLERY_REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK) {
            final Uri videoUri = data.getData();
            getContentResolver().takePersistableUriPermission(
                    videoUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            ((PhotosViewModel) mModel).videoChosen(videoUri);
        }

Finally, after these changes I managed to get the thumbnail with:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
    try {
         Bitmap thumbnail =
                    getApplication().getContentResolver().loadThumbnail(
                                    uri, new Size(300, 300), null);

         } catch (IOException e) {}

AND I was able to access the video using

getContentResolver().openInputStream(Uri.parse(videoURI))

as well as use the URI in a videoView to play the video. Hope this helps a bit.

Related