Android10:contentResolver.delete()无法从文件系统中删除文件
创始人
2024-08-20 08:00:04
0

在Android 10及更高版本中,使用contentResolver.delete()方法删除文件可能无法从文件系统中删除文件。这是因为Android 10及更高版本对于应用程序对共享存储的访问进行了更严格的限制。为了在Android 10及更高版本中删除文件,需要使用Storage Access Framework(SAF)。

以下是一个示例代码,演示如何通过SAF删除文件:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Downloads.TITLE + "=?";
    String[] selectionArgs = new String[]{fileName};
    Cursor cursor = resolver.query(uri, null, selection, selectionArgs, null);
    if (cursor != null && cursor.moveToFirst()) {
        long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID));
        Uri contentUri = ContentUris.withAppendedId(uri, id);
        resolver.delete(contentUri, null, null);
        cursor.close();
        return true;
    }
} else {
    // Use contentResolver.delete() for older Android versions
    // ...
}

在Android Q及更高版本中,您需要执行以下步骤:

  • 获取ContentResolver
  • 获取MediaStore.Downloads.EXTERNAL_CONTENT_URI(或其他所需的URI)
  • 决定要删除哪个文件,并使用MediaStore.Downloads.TITLE在Uri上执行查询
  • 如果查询返回结果,则从Cursor获取_id,并使用ContentUris.withAppendedId()获取对应的Uri
  • 使用ContentResolver.delete()删除文件

对于旧版本的Android,可以继续使用contentResolver.delete()方法删除文件。

相关内容

热门资讯

透视解谜!wepoker怎么挂... 透视解谜!wepoker怎么挂底牌,wepoker买脚本靠谱吗-其实是真的有辅助工具(哔哩哔哩)1、...
透视了解!德普之星怎么设置埋牌... 透视了解!德普之星怎么设置埋牌,德普之星辅助器app-原来真的是有辅助神器(哔哩哔哩)1、免费脚本咨...
透视详情!aa poker透视... 透视详情!aa poker透视软件,aapoker ai插件-本来存在有辅助攻略(哔哩哔哩)能透视中...
透视专业!wepoker挂,w... 透视专业!wepoker挂,wepoker怎么设置透视-确实真的有辅助神器(哔哩哔哩)运辅助工具,进...
透视普及!wepoker怎么设... 透视普及!wepoker怎么设置盖牌,wepoker怎么下载游戏-一直一直都是有辅助软件(哔哩哔哩)...
透视曝光!wepoker免费脚... 透视曝光!wepoker免费脚本咨询,wepoker轻量版辅助-都是一直都是有辅助方法(哔哩哔哩)1...
透视解密!wepoker有透视... 透视解密!wepoker有透视功能吗,htx矩阵wepoker辅助-确实真的有辅助脚本(哔哩哔哩)1...
透视有挂!wpk辅助软件,wp... 透视有挂!wpk辅助软件,wpk透视辅助靠谱吗-一直真的有辅助教程(哔哩哔哩)1、玩家可以在线上大神...
透视分享!aapoker怎么控... 透视分享!aapoker怎么控制牌,aapoker ai插件-切实有辅助方法(哔哩哔哩)1、免费脚本...
透视解迷!werplan外挂,... 透视解迷!werplan外挂,智星菠萝辅助-真是真的是有辅助技巧(哔哩哔哩)1、很好的工具软件,可以...