简体   繁体   English

MediaStore.Audio.Media.DATA 可以抛出无效路径吗?

[英]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.一切正常,直到我开始在setDataSource()行中获取java.lang.IllegalArgumentException ,经过一番挖掘后,我得出结论,使用int pathColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA)指向的文件不再位于该位置,我认为这就是导致问题的原因。

Invalid path: "/storage/emulated/0/zedge/.../Alarm_Clock.mp3"无效路径:“/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 thisPath 实际上只是一个字符串

String thisPath = musicCursor.getString(pathColumn);

But pathColumn is initilized here:但是 pathColumn 在这里初始化:

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 05-15 13:55:50.181 3616-3616/com.bot.lit E/AndroidRuntime:致命异常:主进程:com.bot.lit,PID:3616 java.lang.RuntimeException:无法启动活动组件信息{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) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517) 在 android.os.Handler.dispatchMessage(Handler.java:111) 在 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) 由: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) ` 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) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2559) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2672) 在 android.app.ActivityThread.access$1100(ActivityThread.java:180) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517) 在 android.os.Handler.dispatchMessage(Handler.java:111)在 android.os.Looper.loop(Looper.java:207) 在 android.app.ActivityThread.main(ActivityThread.java:5856) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal .os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) 在 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我得出的结论是,使用int pathColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA)从光标返回的路径指向的文件不再位于该位置

That is entirely possible, as the MediaStore index may be out of date.这是完全可能的,因为MediaStore索引可能已过时。 It is also possible that DATA points to something for which you do not have read access. DATA也可能指向您无权读取的内容。 I believe there are scenarios in which DATA is null .我相信在某些情况下DATAnull

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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