[英]Doesn't Access to external storage in API-29
以下代碼在較低的 API-29 中運行良好。 但是當使用 API-29 時,它會拒絕對外部存儲的訪問。
public void persistFile(byte[] bytes, String path, String fileName) throws IOException {
OutputStream fOut = null;
File picDirectory = new File(path);
File file = new File(path, fileName);
fOut = new FileOutputStream(file);
fOut.write(bytes);
fOut.flush();
fOut.close();
}
AndroidManifest.xml
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
在 Android 10 上,默認情況下,應用私有目錄之外的文件路徑毫無價值,您可以暫時啟用 requestLegacyExternalStorage,直到 Android 11(現在處於預覽版中)
請參閱https://developer.android.com/training/data-storage
展望未來,最好只使用媒體存儲或存儲訪問框架
可以使用在清單應用程序標簽中添加android:requestLegacyExternalStorage="true"
。
<manifest ... >
<!-- This attribute is "false" by default on apps targeting
Android 10 or higher. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.