从本地媒体获取歌曲的封面图片

时间:2018-10-12 08:57:34

标签: java android cursor media

ContentResolver musicResolver = getContentResolver();
        Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri,null,null,null,null);

   if(musicCursor!=null && musicCursor.moveToFirst()){
        int idColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
        int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int artistColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
        int imageColumn = musicCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);

        //Add songs to the array list
        do{
            String theArt = musicCursor.getString(imageColumn);
            Bitmap image = BitmapFactory.decodeFile(theArt);
            long songId = musicCursor.getLong(idColumn);
            String songTitle = musicCursor.getString(titleColumn);
            String songArtist = musicCursor.getString(artistColumn);

            songsArrayList.add(new Songs(songId,songTitle,songArtist,image));

        }while (musicCursor.moveToNext())

我想从本地存储中获取歌曲的封面图片,我不知道该怎么做,这样我就可以得到

  

int imageColumn =   musicCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);

在互联网上获取专辑封面的这段代码在我从中获取字符串时显示错误。在访问列-1时,我认为它没有获得正确的列来获取图像,请让我知道如何以上述代码的方式从媒体获取图像

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.downloadbro.mumusic, PID: 10877
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.downloadbro.mumusic/com.downloadbro.mumusic.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2755)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2816)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6383)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
 Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetString(Native Method)
    at android.database.CursorWindow.getString(CursorWindow.java:438)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
    at android.database.CursorWrapper.getString(CursorWrapper.java:137)
    at com.downloadbro.mumusic.MainActivity.getSongList(MainActivity.java:80)
    at com.downloadbro.mumusic.MainActivity.onCreate(MainActivity.java:45)
    at android.app.Activity.performCreate(Activity.java:6861)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)

这可能会给我专辑艺术,但我不希望媒体艺术对我有这种作用

0 个答案:

没有答案