[英]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.我的应用程序需要备份 WhatsApp 状态、语音笔记和图像的功能。 As you know after Android Q google enforcing to access external media files using MediaStore API.
如您所知,在 Android Q 谷歌强制使用 MediaStore API 访问外部媒体文件之后。
WhatsApp also moved their file to /Android/media/com.whatsapp/WhatsApp
. WhatsApp 还将他们的文件移动到
/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.我尝试使用
MANAGE_EXTERNAL_STORAGE
权限它工作正常,但备份这些文件不是应用程序的核心功能,所以我认为谷歌不会让我使用这个权限。
I wonder if there any way to read those files using MediaStore.VOLUME_EXTERNAL?我想知道是否有任何方法可以使用 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.要阅读 whatsapp 状态,您不需要做更多的事情,只需更改其路径即可。
Also you don't need to use MANAGE_EXTERNAL_STORAGE permission.您也不需要使用 MANAGE_EXTERNAL_STORAGE 权限。 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.这也适用于 android 11。 And I only ask for "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE" like before.
我只像以前一样要求“READ_EXTERNAL_STORAGE”和“WRITE_EXTERNAL_STORAGE”。
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.所以现在你需要检查“WhatsApp”文件夹是否在根存储中可用(在它之前),如果不可用则需要检查我上面提到的路径。
Let me know if you didn't understand or not works.如果您不理解或不起作用,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.