简体   繁体   中英

Query ContentProvider on Emulator with Android 10 (API 29)

I use the following Java code for querying audio files from Android Studio Emulator with api27. Unfortunately this code not works for api 29.

        String[] projection = {
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Images.Media.DATA,
            MediaStore.Audio.Media.TRACK,
            MediaStore.Audio.Media.YEAR
    };
    uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Log.i(TAG, "Querying media...");
    Log.i(TAG, "URI: " + uri.toString());
    // Perform a query on the content resolver. The URI we're passing specifies that we
    // want to query for all audio media on external storage (e.g. SD card)
    Cursor cur = getContentResolver().query(uri, null,
            MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
    Log.i(TAG, "Query finished. " + (cur == null ? "Returned NULL." : "Returned a cursor."));

Manifest.xml

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="28"/>

I can't find any hint in the documentation what has changed between api28 and api29 regarding MediaStore query.

Please help, Thanks GGK

Remove android:maxSdkVersion="28" from your <uses-permission> elements. You need READ_EXTERNAL_STORAGE — including the runtime permission — if you intend to query the MediaStore for content from apps other than your own.

You can query MediaStore without READ_EXTERNAL_STORAGE , but it will only return items that you inserted yourself into the MediaStore .

See this blog post for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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