Android11Api30报java.lang.SecurityException异常
创始人
2024-08-20 10:00:10
0

在 Android 11 Api 30 上,若应用程序使用了 File 和 ProcessBuilder 等需要访问系统文件的 API,那么会出现 java.lang.SecurityException 异常。这是因为 Android 11 在文件系统方面进行了更严格的权限限制。

为了解决这个问题,可以使用新的 API 而不是 File 和 ProcessBuilder。例如,可使用 Storage Access Framework(SAF) API 来读取/写入文件。以下是一个使用 SAF API 来写入文件的示例:

Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TITLE, "file.txt");

startActivityForResult(intent, WRITE_REQUEST_CODE);

@Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { if (resultCode == Activity.RESULT_OK) { if (requestCode == WRITE_REQUEST_CODE) { Uri uri = null; if (resultData != null) { uri = resultData.getData(); try { OutputStream outputStream = getContentResolver().openOutputStream(uri); outputStream.write("Hello, world!".getBytes()); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

这里,我们创建了一个 ACTION_CREATE_DOCUMENT 意图,并使用 startActivityForResult() 方法启动它。用户将在文档选择器中选择一个文件,并授予我们访问文件的权限。在 onActivityResult() 回调方法中,我们使用 getContentResolver().openOutputStream(uri) 方法获取文件的 OutputStream,然后将数据写入文件中。

请注意,这只是一个示例。实际上,您需要根据您的需求进行适当的修改。

相关内容

热门资讯

第二分钟辅助!wepoker插... 第二分钟辅助!wepoker插件功能辅助器,wepoker私人局透视插件,妙招教程(有挂细节)1、金...
六分钟辅助!wepoker透视... 六分钟辅助!wepoker透视是真的吗,wpk俱乐部辅助器,阶段教程(发现有挂)1、wepoker透...
第四分钟辅助!wepoker有... 第四分钟辅助!wepoker有没有透视方法,wepoker有机器人吗,妙计教程(有挂攻略)1、用户打...
第3分钟辅助!hh poker... 第3分钟辅助!hh poker透视器下载,德普之星透视,技法教程(有挂功能)在进入hh poker透...
第1分钟辅助!wepoker数... 第1分钟辅助!wepoker数据分析,we-poker有人玩吗,法门教程(有挂助手)1、we-pok...
第一分钟辅助!wepoker破... 第一分钟辅助!wepoker破解器,菠萝辅助器免费版的特点,经验教程(有挂规律)1、玩家可以在菠萝辅...
5分钟辅助!约局吧德州真的有透... 5分钟辅助!约局吧德州真的有透视挂吗,we-poker软件,妙计教程(今日头条)约局吧德州真的有透视...
第九分钟辅助!wpk插件,hh... 第九分钟辅助!wpk插件,hh poker辅助有用吗,资料教程(确实有挂)1、首先打开hh poke...
第三分钟辅助!wepoker透... 第三分钟辅助!wepoker透视脚本苹果版,pokemmo辅助器,教程书教程(确实有挂)1、完成we...
第三分钟辅助!哈糖大菠萝能开挂... 第三分钟辅助!哈糖大菠萝能开挂吗,aapoker脚本,法门教程(有人有挂)1、超多福利:超高返利,海...