How to use MediaMetadataRetriever SetDataSource fo

2019-03-06 09:43发布

问题:

I'm trying to extract Album art from MP3 file But I not able to give proper location of song, which I can give it to SetDataSource method. I tried everything but still it gives me error IllegalArgumentException.

It all happens when this line metaRetriver.setDataSource(MainActivity.this, Uri.parse("/sdcard/song.mp3")); gets executed

My Code.

public class MainActivity extends AppCompatActivity {

MediaMetadataRetriever metaRetriver;
byte[] art;
ImageView album_art;

Bitmap songImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getInit();

    metaRetriver = new MediaMetadataRetriever();
    metaRetriver.setDataSource(MainActivity.this, Uri.parse("/sdcard/song.mp3"));


    art = metaRetriver.getEmbeddedPicture();

    if (art != null) {
        songImage = BitmapFactory
                .decodeByteArray(art, 0, art.length);
        album_art.setImageBitmap(songImage);
    } else {
        String error = "art is null";
        Log.i("lolol", error);
    }

}

public void getInit() {

    album_art = (ImageView) findViewById(R.id.album_art);

}
}

Can anyone solve this problem.

回答1:

From the Android Documentation:

Throws IllegalArgumentException if the Uri is invalid

Double check that the song.mp3 file is in the right place, and the app has permission to see it.