![](/img/trans.png)
[英]MediaStore.Audio.Media.EXTERNAL_CONTENT_URI is deprecated for api 29+?
[英]MediaStore Audio Column DATA Is Deprecated
大家好,我正在尝试获取 android api level 29 ie, Q
但问题是DATA 已被deprecated
,因此如何解决此问题并使用absolute path (ex: storage/sdcard/music.mp3)
我们可以将嵌入的图片获取到该音频,但在内容 uri( content://
)中,我们可以通过附加文件的 id 使用_ID Column
来获取它。但是如果我们无法获得绝对路径,那么如何使用content://
Uri播放音频文件? 我使用的以前的代码是:
public ArrayList<Song> FolderFilesList(Context context, String FolderPath) {
//folder-exclude-sub-folders
ArrayList<Song> songs = new ArrayList<>();
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.TRACK,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.DISPLAY_NAME,
};
String selection = MediaStore.Audio.Media.DATA + " LIKE ? AND " + MediaStore.Audio.Media.DATA + " NOT LIKE ? ";
String[] selectionArgs = new String[]{
"%" + FolderPath + "%",
"%" + FolderPath + "/%/%"
};
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
columns(), selection, selectionArgs, null);
if (cursor.moveToFirst()) {
do {
Song song = new Song(
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)),
cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.TRACK)),
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
);
//File f = new File(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
// if (f.isFile() && f.exists()) {
songs.add(song);
//}
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
return songs;
}
您现在将无法获得绝对路径,但您可以获取内容 uri 并使用该内容 uri 表示 ( content://
) 并使用它使用以下代码将文件写入 package 文件夹的缓存目录中:
String filePath = new File(resolver.openFileDescriptor(contentUri, "r");
然后使用 InputStream 和 OutputStream 将其存储在缓存目录中,并将此 filePath 用作文件。
在 Manifest 文件的应用程序中使用此行
android:requestLegacyExternalStorage="true"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.