Java 为什么当其他人都很好时,ALBUM_ART是MediaStore中唯一返回空值的字段?
检索相册插图路径时遇到问题。我的存储权限不会有问题,因为我可以获取所有其他字段,所以我想知道可能是什么问题。我承认我对ContentResolver和MediaStore有点陌生。我只需要在BitmapFactory.decodeFile()中使用的路径。下面是代码,后面是日志输出 上下文(如果有帮助) 从唱片集_ID=209检索唱片集数据的测试方法(“格林日万岁”) 输出Java 为什么当其他人都很好时,ALBUM_ART是MediaStore中唯一返回空值的字段?,java,android,android-cursor,mediastore,android-external-storage,Java,Android,Android Cursor,Mediastore,Android External Storage,检索相册插图路径时遇到问题。我的存储权限不会有问题,因为我可以获取所有其他字段,所以我想知道可能是什么问题。我承认我对ContentResolver和MediaStore有点陌生。我只需要在BitmapFactory.decodeFile()中使用的路径。下面是代码,后面是日志输出 上下文(如果有帮助) 从唱片集_ID=209检索唱片集数据的测试方法(“格林日万岁”) 输出 它只是存放在别的地方吗?我是否需要以特定的方式检索它,因为它不像其他字段那样是字符串(如果它返回的不是路径)?我最近在我的
它只是存放在别的地方吗?我是否需要以特定的方式检索它,因为它不像其他字段那样是字符串(如果它返回的不是路径)?我最近在我的应用程序中注意到,album arts在以前使用类似代码的地方开始出现空白 我查了一下,有关于专辑艺术的新信息 此常量在API级别29中被弃用。应用程序可能没有 直接访问此路径的文件系统权限。而不是尝试 若要直接打开此路径,应用程序应使用 ContentResolver#加载缩略图以获得访问权限 因此,我尝试将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();
}
}
String filePath=null;
如果(Build.VERSION.SDK_INT
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/
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();
}
}