英文:
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 <= 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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论