gpt4 book ai didi

android - 为什么不需要 READ_EXTERNAL_STORAGE 权限请求来从外部存储读取文件?

转载 作者:太空狗 更新时间:2023-10-29 13:08:44 28 4
gpt4 key购买 nike

我正在模拟器上测试我的应用。我有一个导出功能,可以在外部存储的下载目录中创建和写入文件。我还有一个导入功能,可以从外部存储的下载目录中读取文件。

来自 Android documentation :

If the device is running Android 5.1 or lower, or your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all.

If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.

我的模拟器在 Android 6.0 上运行,我的应用程序的目标 SDK 是 25,因此我还必须在应用程序运行时请求它需要的每个危险权限。我这样做是为了导出功能,一切正常。但是,当我实现导入功能时,我没有在运行时请求权限。奇怪的是,即使在运行时没有请求和授予 READ_EXTERNAL_STORAGE,我仍然能够从外部存储的许可中读取。根据此 Android documentation,READ_EXTERNAL_STORAGE 是一个危险的权限.

为了验证,我确保在开始使用该功能之前禁用权限,并且在使用完成后,我再次验证权限仍然未被授予。虽然我对这种行为很满意,因为它在运行时无需我请求许可就可以工作,但根据文档,我不认为这种行为是预期的。这就是为什么我想知道是什么原因造成的,并在发布应用的任何更改之前找出问题所在。

这是我的 list 的代码 fragment :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我选择要读取的文件的代码 fragment :

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(intent, GET_FILE_RESULT_CODE);

我读取从上面的代码 fragment 中选择的文件的代码 fragment (exportFile 只是来自 onActivityResult 的 URI):

    BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(context.getContentResolver().openInputStream(exportFile)));
String line;
// Skip first header line
br.readLine();
while ((line = br.readLine()) != null) {...}

谢谢!

最佳答案

有很好的解释here ,

READ_EXTERNAL_STORAGE

Provides protected read access to external storage. In Android 4.1 by default all applications still have read access. This will be changed in a future release to require that applications explicitly request read access using this permission. If your application already requests write access, it will automatically get read access as well. There is a new developer option to turn on read access restriction, for developers to test their applications against how Android will behave in the future.

简而言之,READ_EXTERNAL_STORAGE 只存在于 Jelly Bean(16 级)。因此,除非您使用的是 Jelly Bean 手机并设置了开发者选项“Protect USB storage”,否则这不会成为问题。

关于android - 为什么不需要 READ_EXTERNAL_STORAGE 权限请求来从外部存储读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43860268/

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