每次调试时,Cursor对象都会出错

时间:2018-05-06 16:21:38

标签: android

我正在尝试在Android平台上制作音乐播放器应用程序。我要做的是检索歌曲的专辑封面并使用ImageView显示它。但每当我运行代码时,我从歌曲中取出歌曲信息的cursor对象给出了错误(它在我们初始化它的语句中给出了错误。这是我的代码,

public class MainActivity extends AppCompatActivity {

    ImageView album_art;
    Drawable drawable;

    long x;

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

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


        Log.i("Before", "before the cursor");
        Cursor cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null,
                null, null,
                null);
        Log.i("after", "after the cursor");

        if (cursor.moveToFirst()) {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
        }

        cursor.close();

        x = cursor.getColumnIndex(MediaStore.Audio.Albums._ID);

        album_art.setImageBitmap(getAlbumart(x));
    }

    public Bitmap getAlbumart(Long album_id) {
        Bitmap bitmap_AlbumArt = null;

        try {
            final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");

            Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

            ParcelFileDescriptor pfd = getApplicationContext().getContentResolver().openFileDescriptor(uri, "r");

            if (pfd != null) {
                FileDescriptor fd = pfd.getFileDescriptor();
                bitmap_AlbumArt = BitmapFactory.decodeFileDescriptor(fd);
            }
        } catch (Exception e) {

        }
        return bitmap_AlbumArt;
    }
}

如果我的代码中有任何错误,请更正我:)

0 个答案:

没有答案