I have made an application that lets the user pick an mp4 file, a date and a time. Once set, the app starts a foreground service that should play the media file using an implicit intent when the time comes.
Code for letting user pick the media file:
private void openFile() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("video/mp4");
startActivityForResult(intent, GET_FILE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == GET_FILE && resultCode == AppCompatActivity.RESULT_OK) {
// The resultData contains a URI for the document or directory that the user selected.
media_uri = resultData.getData();
Log.e("Activity_Files", "Received Uri: " + media_uri.toString());
}
For a sample run on my phone running API 23, from the logcat I get
03-29 21:06:21.529 14630-14630/com.basulabs.mahalaya E/Activity_Files: Received Uri: content://com.android.externalstorage.documents/document/6333-6461%3AVID-20170319-WA0003.mp4
which shows Uri has been received successfully.
Now from the foreground service, I play the media file.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(media_uri, "video/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.e("Mahalaya Service", "Time up!");
if (intent.resolveActivity(getPackageManager()) != null) {
Log.e("Mahalaya Service", "Intent fired.");
startActivity(intent);
}
}
Again from the logcat,
03-29 21:07:00.038 14630-15146/com.basulabs.mahalaya E/Mahalaya Service: Time up!
03-29 21:07:00.048 14630-15146/com.basulabs.mahalaya E/Mahalaya Service: Intent fired.
On my phone, I see a list of apps that can play the mp4 file. I choose the inbuilt media player, which can normally play mp4 files. Now I get an error.
From the logcat,
03-29 21:07:05.088 15199-15199/? E/CloudUtil: checkCloudAgentExistence() - NameNotFoundException
03-29 21:07:05.088 15199-15199/? E/MoviePlayer: initIntentInfo : LaunchType unknown!!! reset player info
03-29 21:07:05.088 15199-15199/? E/VUtils: resetPlayerInfo Normal List
03-29 21:07:05.178 15199-15199/? E/MoviePlayer: onResume. Intent is not vaild to play a video. finish()
Can anyone tell me why the intent is not valid?
Update 1:
I tried on an emulator running API 27 using VLC for Android. Logcat throws a different error:
2020-03-30 00:17:07.420 23672-23689/? E/VLC/LibVLC/Util: WARNING: Can't find shared library
2020-03-30 00:17:10.791 23672-23691/? E/VLC/FileUtils: Permission is no longer valid
Update 2:
I just found out a horrible problem in my app. The structure of my app is as follows:
Activity1 --> Takes the media Uri, starts Activity2
Activity2 --> User selects a date, starts Activity3
Activity3 --> User selects a time, starts MyService
MyService --> A foreground service to play the media using an intent at the correct time
Now, on testing, I found that if Activity1 starts MyService directly (I set the date and time programmatically), the media can play properly.
But if Activity1 starts Activity2, which in turn starts MyService directly (again I set the time), the media cannot play!!
Remember I call Activity2 with the same intent flags with which I call MyService:
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
This is inevitably the problem indicated by @CommonsWare in this link. I thought that if I start activities with the same intent flags, then the permission will remain valid. But that is NOT so!