首页 > 解决方案 > 为什么 ALBUM_ART 是唯一一个在其他人都很好的情况下从 MediaStore 返回 null 的字段?

问题描述

我在检索专辑插图路径时遇到问题。我的存储权限不可能是问题,因为我可以获取所有其他字段,所以我想知道问题可能是什么。我承认我对 ContentResolvers 和 MediaStore 有点陌生。我只需要一个在 BitmapFactory.decodeFile() 中使用的路径。代码如下,后跟 Log 输出。

上下文(如果有帮助)

- Method is called from SongRoomDatabase.java (not an activity) which extends RoomDatabase. 
- The context is passed from the MainActivity to the DB through the DB constructor.

从 ALBUM_ID = 209 检索专辑数据的测试方法(Greenday 的“Viva la Gloria!”)

public static void getCoverArtTest(){
        Cursor cursor = mContext.getContentResolver().query(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                new String[] {
                        MediaStore.Audio.Albums._ID,
                        MediaStore.Audio.Albums.ALBUM,
                        MediaStore.Audio.Albums.ALBUM_ART,
                        MediaStore.Audio.Albums.ARTIST
                },
                MediaStore.Audio.Albums._ID+ "=?",
                new String[] {String.valueOf(209)},
                null);

        if (cursor != null && cursor.moveToFirst()) {
            String path = "";
            path += cursor.getString(0) + "/";
            path += cursor.getString(1) + "/";
            path += cursor.getString(2) + "/";
            path += cursor.getString(3) + "/";
            // do whatever you need to do
            Log.d("artworkTag", "Album art path: " + path);
        }
    }

输出

 Album art path: 209/21st Century Breakdown/null/Green Day/

它只是存储在其他地方吗?我是否需要以特定方式检索它,因为它不像其他字段那样是字符串(如果它返回的不是它的路径)?

标签: javaandroidandroid-cursormediastoreandroid-external-storage

解决方案


我最近注意到,在我的应用程序中,专辑艺术开始出现空白,之前它使用类似的代码。

我检查了开发人员参考,有关于 ALBUM_ART 的新信息

此常量在 API 级别 29 中已弃用。应用程序可能没有文件系统权限来直接访问此路径。应用程序不应尝试直接打开此路径,而应使用 ContentResolver#loadThumbnail 来获得访问权限。

因此,我尝试将 targetSdkVersion 版本改回 28,但在使用 android 10 Q 即 api 29 的设备上运行时仍然没有好处。

所以不幸的是,为了支持更新的机器人,我们需要使用类似这样的东西:

String filePath = null;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
    Cursor albumCursor = context.getContentResolver().query(
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Albums.ALBUM_ART},
            MediaStore.Audio.Media._ID + " = " + albumId,
            null,
            null);
    if (albumCursor != null) {
        if (albumCursor.moveToFirst()) {
            filePath = albumCursor.getString(1);
        }
        albumCursor.close();
    }
} else {
    Uri albumArtUri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, Long.parseLong(albumId));
    try {
        Bitmap bitmap = context.getContentResolver().loadThumbnail(albumArtUri, new Size(1024, 1024), null);

        File art = new File(context.getCacheDir(), "albumart" + albumId + ".jpg");
        art.createNewFile();
        FileOutputStream fos = new FileOutputStream(art);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
        fos.flush();
        fos.close();
        filePath = art.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

推荐阅读