MediaStore音频列DATA已被废弃。

问题描述 投票:0回答:1

大家好,我想在安卓系统中获取音频文件的绝对路径。api level 29 i.e, Q 但问题是 数据是 deprecated 那么,如何解决这个问题,并同时使用的是 absolute path (e.x : storage/sdcard/music.mp3) 我们可以将图片嵌入到该音频中,但在内容URI中(content://),我们可以用 _ID Column 但是,如果我们不能得到绝对路径,那么如何播放一个音频文件使用 content:// 乌里 以前的代码,我使用的是。

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;
    }
java android android-studio audio mediastore
1个回答
1
投票

你现在无法获得绝对路径,但你可以获得内容尿素,使用该内容尿素意味着(content://)并使用它在你的包文件夹的缓存目录下使用下面的代码写入文件。

String filePath = new File(resolver.openFileDescriptor(contentUri, "r");

然后使用InputStream和OutputStream将其存储在cache目录下,并将此filePath作为一个文件。

© www.soinside.com 2019 - 2024. All rights reserved.