gpt4 book ai didi

android - 如何在 Android Q 中请求删除不属于自己的文件

转载 作者:行者123 更新时间:2023-11-29 23:06:09 24 4
gpt4 key购买 nike

在 Android Q 中,非默认文件管理器或图库的应用只能修改和/或删除它们拥有的图像文件,也就是应用创建的图像文件。

授予读/写权限不允许修改或删除任何不属于应用程序的文件。

这意味着不仅无法访问其他应用创建的文件,而且如果某个应用被卸载然后重新安装,那么该应用将失去对该应用之前创建的所有公共(public)文件的所有权 .因此,重新安装后将无法再修改或删除它们。

当想要修改 1 个图像文件或删除大量图像文件时,这些图像文件以前属于某个应用程序,但由于重新安装而失去了所有权,那么实现此类操作的过程是什么(删除或修改)?

更好的解决方案是不使用 SAF 文件选择器,以避免要求用户通过 SAF 选择和授予位置。

如果唯一的解决方案是使用SAF文件选择器,那么如何触发直接提示删除一组已知的特定文件而不请求树访问,也不必告诉用户浏览、搜索和做是他自己吗?

最佳答案

我的最终结论。

对于 >= 29 的 API,在没有用户交互的情况下无法删除非拥有的文件,并且没有办法绕过这个事实。

Android 10/Q (API 29) 中,一个 RecoverableSecurityException必须被捕获,然后请求用户许可,最后如果许可则执行删除。

Android 11/R (API 30) 中得到了极大的改进。可以批量删除甚至合并同一批中已有的文件。请求后无需处理任何事情,如果用户允许,系统会负责删除。限制是它只能处理媒体文件(图像、视频、音频)。对于其他文件类型,将抛出 IllegalArgumentException 并显示以下消息:“所有请求的项目必须由特定 ID 引用”,( check this message in MediaStore source code )。

请注意,在 API 30 中有一个新的 MANAGE_EXTERNAL_STORAGE权限,但它的使用需要在开发人员控制台中执行额外的步骤,例如解释为什么需要权限。

例子:

public static void delete(final Activity activity, final Uri[] uriList, final int requestCode)
throws SecurityException, IntentSender.SendIntentException, IllegalArgumentException
{
final ContentResolver resolver = activity.getContentResolver();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
{
// WARNING: if the URI isn't a MediaStore Uri and specifically
// only for media files (images, videos, audio) then the request
// will throw an IllegalArgumentException, with the message:
// 'All requested items must be referenced by specific ID'

// No need to handle 'onActivityResult' callback, when the system returns
// from the user permission prompt the files will be already deleted.
// Multiple 'owned' and 'not-owned' files can be combined in the
// same batch request. The system will automatically delete them
// using the same prompt dialog, making the experience homogeneous.

final List<Uri> list = new ArrayList<>();
Collections.addAll(list, uriList);

final PendingIntent pendingIntent = MediaStore.createDeleteRequest(resolver, list);
activity.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, null, 0, 0, 0, null);
}
else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q)
{
try
{
// In Android == Q a RecoverableSecurityException is thrown for not-owned.
// For a batch request the deletion will stop at the failed not-owned
// file, so you may want to restrict deletion in Android Q to only
// 1 file at a time, to make the experience less ugly.
// Fortunately this gets solved in Android R.

for (final Uri uri : uriList)
{
resolver.delete(uri, null, null);
}
}
catch (RecoverableSecurityException ex)
{
final IntentSender intent = ex.getUserAction()
.getActionIntent()
.getIntentSender();

// IMPORTANT: still need to perform the actual deletion
// as usual, so again getContentResolver().delete(...),
// in your 'onActivityResult' callback, as in Android Q
// all this extra code is necessary 'only' to get the permission,
// as the system doesn't perform any actual deletion at all.
// The onActivityResult doesn't have the target Uri, so you
// need to cache it somewhere.
activity.startIntentSenderForResult(intent, requestCode, null, 0, 0, 0, null);
}
}
else
{
// As usual for older APIs

for (final Uri uri : uriList)
{
resolver.delete(uri, null, null);
}
}
}

关于android - 如何在 Android Q 中请求删除不属于自己的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56496769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com