Issue
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)
Solution
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.
Answered By - Hoang Truong
Answer Checked By - Cary Denson (JavaFixing Admin)