ALBUM_ART是唯一一个从MediaStore返回null的字段,其他字段都正常。

huangapple go评论46阅读模式
英文:

Why is ALBUM_ART the only field that returns null from MediaStore when others are fine?

问题

I'm having trouble retrieving the album artwork path. My storage permissions can't be the problem since I can fetch all other fields, so I'm wondering what the problem might be. I'll admit I'm a bit new to ContentResolvers and the MediaStore. I just need a path to be used in BitmapFactory.decodeFile(). The code is below, followed by the Log output.

Context (if it helps)

- 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.

Test method for retrieving album data from ALBUM_ID = 209 ("Viva la Gloria!" by Greenday)

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);
        }
    }

Output

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

Is it just stored somewhere else? Do I need to retrieve it a specific way since it's not a string like the other fields (if it's returning something other than its path)?

英文:

I'm having trouble retrieving the album artwork path. My storage permissions can't be the problem since I can fetch all other fields, so I'm wondering what the problem might be. I'll admit I'm a bit new to ContentResolvers and the MediaStore. I just need a path to be used in BitmapFactory.decodeFile(). The code is below, followed by the Log output.

Context (if it helps)

- 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.

Test method for retrieving album data from ALBUM_ID = 209 ("Viva la Gloria!" by Greenday)

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);
        }
    }

Output

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

Is it just stored somewhere else? Do I need to retrieve it a specific way since it's not a string like the other fields (if it's returning something other than it's path)?

答案1

得分: 1

我最近在我的应用中注意到,专辑封面开始出现为空,而之前使用了类似的代码。

我查看了开发者参考,关于ALBUM_ART有新的信息

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

所以我尝试将targetSdkVersion版本更改回28,但在运行Android 10 Q(API 29)的设备上仍然不起作用。

所以不幸的是,为了支持更新的Android版本,我们需要使用类似以下的方法:

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();
    }
}
英文:

I noticed recently in my app the album arts started coming up empty where it had previously worked with similar code.

I checked the developer reference and there is new information about ALBUM_ART

> This constant was deprecated in API level 29. Apps may not have
> filesystem permissions to directly access this path. Instead of trying
> to open this path directly, apps should use
> ContentResolver#loadThumbnail to gain access.

So I tried changed the targetSdkVersion version back to 28 but still no good when running on a device with android 10 Q which is api 29.

So unfortunately to support newer androids we need to use something like this instead:

String filePath = null;
if (Build.VERSION.SDK_INT &lt;= 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 + &quot; = &quot; + 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(), &quot;albumart&quot; + albumId + &quot;.jpg&quot;);
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();
}
}

huangapple
  • 本文由 发表于 2020年7月31日 05:45:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63181820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定