1

I let the user select a video from the gallery then I save the uri of the video for future use. But after I close the app and reopen it, the video can't play anymore This is intent i use for select video from gallery

private void pickVideoFromGallery() {
        Intent opengalleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        opengalleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
        opengalleryIntent.setType("video/*");
        opengalleryIntent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                | Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(opengalleryIntent, RESULT_LOAD_VIDEO);
    } 

I save the video uri as a string

    String videoPath = data.getData().toString();

Use:

    String videoPath= getIntent().getStringExtra("videoPath"); //From PendingIntent in ForegroundService
            viFriendCall.setVideoURI(Uri.parse(videoPath));
            viFriendCall.setOnPreparedListener(mp -> mp.setLooping(true));
            viFriendCall.start();

And this is the error message i get

E/MediaPlayerNative: Unable to create media player
W/VideoView: Unable to open content: content://com.android.providers.media.documents/document/video%3A9922
    java.io.IOException: setDataSource failed.: status=0x80000000
        at android.media.MediaPlayer.nativeSetDataSource(Native Method)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1209)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1196)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1107)

enter image description here

Hoagf
  • 28
  • 4

2 Answers2

2

This error appears on devices android 11 and above. Originally admitted that after closing the app your uri no longer has access to the file.This is a new update from android 11 version, you can read it here

There's one simple solution to it. If you want to access the file for later use in your app, the entry copying it from the original location to your app's cache or files directory.

You'll find plenty of documents on it. Just save your file into the files directory when you first get the URI of your files.

0

You have to use caching in Android to save your data for future use

  1. Room data (can be use for complex data such as object, classes): https://developer.android.com/training/data-storage/room

  2. Data store (can be use to store the primitive data types) https://developer.android.com/topic/libraries/architecture/datastore?gclsrc=aw.ds&gclid=Cj0KCQjwmPSSBhCNARIsAH3cYgaKcy6vvBjj2320H6OSog7Wdk8eBVtFrpwsBO3fuNJYm6uc2GKFYGEaAmUrEALw_wcB

And the problem with your code is when user close the app, all variable that you initialized will be cleared from the memory and when user comes back to the app all variable will be initialized from fresh

Nitin Prakash
  • 927
  • 9
  • 16
  • Thanks, but maybe you misunderstood what I meant. i save data as string and send to Foreground Service then pass to Activity B with pendingIntent. And activity B has received this uri string. but when i passed this string to play the video it didn't work – Hoagf Apr 18 '22 at 14:08
  • I also passed a uri string of a photo but the image can display normally but the video is not – Hoagf Apr 18 '22 at 14:09
  • Can you share your activities code also? – Nitin Prakash Apr 19 '22 at 07:49