简体   繁体   中英

Is it possible to get WhatsApp media file using MediaStore.VOLUME_EXTERNAL

My app requires a feature that backups WhatsApp status, voice notes, and images. As you know after Android Q google enforcing to access external media files using MediaStore API.

WhatsApp also moved their file to /Android/media/com.whatsapp/WhatsApp . I tried using MANAGE_EXTERNAL_STORAGE permission it works fine, but backing up these files is not the core functionality of the app, so I don't think google going to let me use this permission.

I wonder if there any way to read those files using MediaStore.VOLUME_EXTERNAL?

I tried something like this. I am not sure if this is even possible.

val collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)

val selection= (MediaStore.Files.FileColumns.MEDIA_TYPE + "="
        + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE)
val selectionArgs= arrayOf("%/WhatsApp/Media/.Statuses%")
val cursor = applicationContext.contentResolver.query(
    collection, null, selection, selectionArgs, null)
debug(cursor?.columnCount)
cursor?.close()

it throws an exception.

Caused by: android.database.sqlite.SQLiteException: no such column: media_type

Try this...

To read whatsapp status you don't need to do more things, just changed its path.

Also you don't need to use MANAGE_EXTERNAL_STORAGE permission. This can work with older permission too.

This way I have done

String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/media/"+"com.whatsapp" + "/WhatsApp/Media/.Statuses/"

File directory = new File(wa_path);
    if (!directory.exists()) {
        directory.mkdirs();
    }
    File[] files = directory.listFiles();
    if (files != null) {
        Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
        for (int i = 0; i < files.length; i++) {
            if (!files[i].getName().equals(".nomedia")) {
                your_list.add(files[i]);
            }
        }
    }

This is working in android 11 too. And I only ask for "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE" like before.

So now you need to check whether "WhatsApp" folder is available in root storage(before where it was), if not available then need to check in path I mentioned above.

Let me know if you didn't understand or not works.

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