Android Q:file.mkdirs()返回false [英] Android Q: file.mkdirs() returns false
问题描述
我们有一个应用程序,它使用外部存储来存储一些临时文件:图像,二进制数据.直到最近,用于该代码的代码已经运行了好几年,没有太大的变化.在Android Q上不起作用:
We have an app that uses external storage to store some temporary files: images, binary data. The code for that has been working for a few years without big changes until recently. On Android Q it doesn't work:
File f = new File(Environment.getExternalStorageDirectory().toString() + File.separator + MainActivity.APP_DIR)
f.mkdirs();
// do sth with f
mkdirs
现在仅返回false
.
清单中提供了必需的权限:
Required permission is provided in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
该代码在早期版本的Android上正常运行.这种访问类型在系统级别上有变化吗?如果是这样,解决方法是什么?
The code works fine on previous versions of Android. Is there some system level change to this type of access? If so, what is the workaround?
推荐答案
通过引入范围存储.
从Q beta 4开始,可以 opt 删除该功能的方法:
Since Q beta 4 it's possible to opt-out of that feature by:
- 定位API 28(或更低版本)
- 使用
requestLegacyExternalStorage
清单属性:
- targeting api 28 (or lower)
- using
requestLegacyExternalStorage
manifest attribute:
<manifest ... >
<!-- This attribute is "false" by default on apps targeting Android Q. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
*请注意,我没有测试它,只是引用了文档
*note I haven't tested it, just quoting docs
这篇关于Android Q:file.mkdirs()返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!