0
votes

I have an app that lets users make a list of songs that they choose from their device. They can then choose a song to play and the app will open the song's uri with an Intent so that the default music player is used to actually play the song.

This is working fine if I choose the song with Astro file manager but if I choose a file through google Drive, the Audio section or the Downloads section then the song will not play.

I'm choosing the files with an Intent like so:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
intent = Intent.createChooser(intent, "Select audio or music");

And then retrieving the Uri with data.getData().

To later open the Uri Im using an intent like:

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);

Astro file manager is returning Uris in the form file:///storage/sdcard0/Download/My%20Song.mp3 and these ones open and play fine with the default media app.

Google drive is returning Uris in the form content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D421. When I try to open Uris like that with an Intent using the default media app it shows a popup "Couldn't play the track that you requested".

How do I open a content Uri that Google Drive is returning with the default app for that media type?

Edit: I have also tried explicitly setting the mime type when opening the uri with the Intent:

Intent intent = new Intent(Intent.ACTION_VIEW, audio.getAudioUri());
intent.setType("audio/*");
contentController.startActivity(intent);

When I do this it opens with VLC media player and VLC reports the error "VLC encountered an error with this media. Please try refreshing the media library."

EDIT 2: It's always opening with VLC even though I'm forcing a chooser:

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("audio/*");
Intent chooser = Intent.createChooser(intent, "Play the song with");
//contentController.startActivity(intent);
contentController.startActivity(chooser);

I've even tried Settings->Apps->Reset app preferences and it's still always defaulting to VLC. This is on a Nexus 5 running Android 5.1.

Edit 3: Logcat errors I'm seeing these errors in the logcat window:

04-14 09:26:27.869  29143-29143/com.my.app E/Audio﹕ Permission Denial: opening provider com.google.android.apps.docs.storagebackend.StorageBackendContentProvider from ProcessRecord{2f0a6a28 29143:com.my.app/u0a80} (pid=29143, uid=10080) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
04-14 09:26:27.871  29143-29143/com.my.app E/Audio﹕ Permission Denial: opening provider com.google.android.apps.docs.storagebackend.StorageBackendContentProvider from ProcessRecord{2f0a6a28 29143:com.my.app/u0a80} (pid=29143, uid=10080) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

In the AndroidManifest.xml of the app I do have that specified:

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

Edit 4: Well this appears to be device specific. All of the testing above was done on the Nexus 5. I've just tested on my Nexus 7 (Android 5.0.2) and opening the content uri with the direct Intent rather than the chooser works with no errors.

1
What error message do you see in the Android log (logcat)?cketti

1 Answers

4
votes

The media player needs permission to access the (Google Drive) content provider. Add the flag FLAG_GRANT_READ_URI_PERMISSION to the view intent.

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);