简体   繁体   中英

Can MediaStore.Audio.Media.DATA throw an invalid path?

So, I have some code like this:

            String thisPath = cursor.getString(pathColumn);

            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            mmr.setDataSource(thisPath);

            byte[] data = mmr.getEmbeddedPicture();

            if (data != null) {
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

            } else {
                bitmap = null;
            }

Everything was working fine, until I started getting a java.lang.IllegalArgumentException in the setDataSource() line, after some digging around I got to the conclusion that the path returned from the cursor using int pathColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA) was pointing to a file that's no longer in that location, I think that's what's causing the issue.

Invalid path: "/storage/emulated/0/zedge/.../Alarm_Clock.mp3"

I have set the Storage permissions already, and it works for the rest of the files I'm looking for, I don't even know how I'm getting that path, since there's no file there.

Can anyone help?

Cheers,

EDIT:

thisPath is actually just a String

String thisPath = musicCursor.getString(pathColumn);

But pathColumn is initilized here:

ContentResolver contentResolver = getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {

        int pathColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);

        do {
            String thisPath = cursor.getString(pathColumn);

            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            mmr.setDataSource(thisPath);

            byte[] data = mmr.getEmbeddedPicture();

            if (data != null) {
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

            } else {
                bitmap = null;
            }

            songList.add(new Song(thisId, thisTitle, thisArtist, thisAlbum, bitmap));

        } while (cursor.moveToNext());

StackTrace:

05-15 13:55:50.181 3616-3616/com.bot.lit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.bot.lit, PID: 3616 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bot.lit/com.bot.lit.MainActivity}: java.lang.IllegalArgumentException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2606) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2672) at android.app.ActivityThread.access$1100(ActivityThread.java:180) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5856) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782) Caused by: java.lang.IllegalArgumentException at android.media.MediaMetadataRetriever.setDataSo urce(MediaMetadataRetriever.java:73) at com.bot.lit.MainActivity.getSongList(MainActivity.java:273) at com.bot.lit.MainActivity.onCreate(MainActivity.java:108) at android.app.Activity.performCreate(Activity.java:6347) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2559) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2672) at android.app.ActivityThread.access$1100(ActivityThread.java:180) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5856) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782) `

I got to the conclusion that the path returned from the cursor using int pathColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA) was pointing to a file that's no longer in that location

That is entirely possible, as the MediaStore index may be out of date. It is also possible that DATA points to something for which you do not have read access. I believe there are scenarios in which DATA is null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM