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
How Does One Implement Drag and Drop for Android Marker
How to Copy Selected Files from Android with Adb Pull
How to Use Mediacodec Without Mediaextractor for H264
Speech to Text from Own Sound File
How to Check the Internet Connection Periodically in Whole Application
No Notification Sound When Sending Notification from Firebase in Android
How to Use Textview.Getlayout()? It Returns Null
Resizing Imageview to Fit to Aspect Ratio
Android Can't Record Video with Front Facing Camera, Mediarecorder Start Failed: -19
How to Add a Hint in Spinner in Xml
Creating a Product Sdk: How to Add a Native Lib (.So) and a Jar with the Sdk I am Creating
How to Get the Key from the Value in Firebase
How to Close the Status Bar/Notification Panel After Notification Button Click
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea
Android Audiotrack Playing .Wav File, Getting Only White Noise
How to Get the Dreaded Write_Secure_Settings Permission for My Android App
How to Hide the Share Action (Which Use Most) Icon Near the Share Action Provider