简体   繁体   English

想要列出内部存储和外部存储上的所有音乐文件,但在 Android 10 上无法完成

[英]Want to list all music file on internal storage and external storage, but it cannot be done on Android 10

I am trying to access and list out all the music file on android Q or 10. I can do it with cursor and adding this projection我正在尝试访问并列出 android Q 或 10 上的所有音乐文件。我可以使用光标并添加此投影来完成

private static final String[] EXTERNAL_COLUMNS = new String[]{
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.IS_RINGTONE,
            MediaStore.Audio.Media.IS_ALARM,
            MediaStore.Audio.Media.IS_NOTIFICATION,
            MediaStore.Audio.Media.IS_MUSIC,
            "\"" + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + "\""
    };

but it is working upto api level 28. On android 10 it is showing Caused by: java.lang.IllegalArgumentException: Invalid column "content://media/external/audio/media" .但它工作到 api 级别 28。在 android 10 上它显示Caused by: java.lang.IllegalArgumentException: Invalid column "content://media/external/audio/media"

I am adding this projection in cursorLoader.我在 cursorLoader 中添加了这个投影。 I know there are some behavioral changes in Android 10 api level, but this thing is really freaking me out.我知道 Android 10 api 级别有一些行为变化,但这件事真的让我感到害怕。 Can anyone help me in that.任何人都可以帮助我。 Thanks in advance.提前致谢。

MediaStore.Audio.AudioColumns替换MediaStore.Audio.Media

I only use:-我只使用:-

 private static final String[] EXTERNAL_COLUMNS = new String[]{
            MediaStore.Audio.Media._ID,

    };

as column, and pass null as projection.作为列,并将 null 作为投影传递。

then create uri for each file by然后为每个文件创建 uri

Uri uri=ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)

after that i list all the uri, and get all other data of file through java.之后我列出所有的uri,并通过java获取文件的所有其他数据。 It works, thanks @commonsWare.它有效,谢谢@commonsWare。 It looks difficult, but it is far easy, just give it a try!看起来很难,其实很简单,快来试试吧!

I am adding image fetch code for the reference我正在添加图像获取代码以供参考

fun getAllDataPath(activity: Activity): MutableList<Uri> {
    val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val cursor: Cursor?
    val columnIndexID: Int
    val listOfAllImages: MutableList<Uri> = mutableListOf()
    val projection = arrayOf(MediaStore.Images.Media._ID)
    var imageId: Long
    cursor = activity.contentResolver.query(uriExternal, projection, null, null, null)
    if (cursor != null) {
        columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
        while (cursor.moveToNext()) {
            imageId = cursor.getLong(columnIndexID)
            val uriImage = Uri.withAppendedPath(uriExternal, "" + imageId)
            listOfAllImages.add(uriImage)
        }
        cursor.close()
    }
    return listOfAllImages
}

Please convert images to music and use it for your reference.请将图像转换为音乐并用作参考。

thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM