Mediametadataretriever Setdatasource Throws Illegalargumentexception

Android 10 : API 29 - java.lang.IllegalArgumentException at android.media.MediaMetadataRetriever.setDataSource

this code didn't give a traceback and keep the app working but doesn't work perfectly
it just ignores the image

private byte[] getAlbumArt(String uri) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14){
try {
retriever.setDataSource(uri, new HashMap<String, String>());
} catch (RuntimeException ex) {
// something went wrong with the file, ignore it and continue
}
}
else {
retriever.setDataSource(uri);
}
byte[] art = retriever.getEmbeddedPicture();
retriever.release();
return art;
}

IllegalArgumentException when sending Uri to setDataSource() in MediaMetadataRetriever

I found out the problem was that IllegalArgumentException comes if the Uri was saved in Sharedpreferences without permission to read and write Uri specifically the Uri Iam targeting for a file like audio file .So the method works for e.g if I send file Uri directly , If I save the folder path that has the files in shared pref then extract the Uris and sent them to the method or if I make these permissions to save a file in shared pref then retrieve and send to the method.

so I implemented Recek's answer :-

https://stackoverflow.com/a/46506626/7552894

First I made the intend as ACTION_OPEN_DOCUMENT:-

     intent.setType("audio/*");
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);

Where I added this part after getting the intent first time:-

     this.grantUriPermission(this.getPackageName(), data.getData(), Intent.FLAG_GRANT_READ_URI_PERMISSION);
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // Check for the freshest data.
//noinspection WrongConstant
this.getContentResolver().takePersistableUriPermission(data.getData(), takeFlags);

-saved to shared pref .And retrieved normally.Whats interesting is that the Uri that comes directly from the file looks like so:-content://com.android.externalstorage.documents/document/primary%3ADownload%2FDeath%20Grips%20-%20Get%20Got.mp3

gets result if not saved in shared pref.

However the one got from the new intent looks liks so:- content://com.android.providers.downloads.documents/document/433

the one that can be saved.

Edit

also folder tree Uri can be saved and retrieved without the read Uri permissions ,only file tree needs Uri permission.I tried to get folder tree from Uri tree But that doesn't work in getting the files ,the Uri must come from Intent.

IllegalArgumentException on MediaMetaDataReceiver.setDataSource Android

if I'm not mistaken there was a bug related to MediaMetadataRetriever.

you could try and see if:

metaRetreiver.setDataSource("http://usa8-vn.mixstream.net:8138", new HashMap<String, String>());

fixes your problem.



Related Topics



Leave a reply



Submit