[英]Create to a folder under /sdcard/Download folder in Android API 29 and API 30
我使用此代碼在 /sdcard/Download 下創建一個文件夾
String toLocation = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() +
+ "/MyFolder/example.txt";
File toLocationFile = new File(toLocation);
if (!toLocationFile.getParentFile().exists()) {
toLocationFile.getParentFile().mkdirs();
}
該版本針對 API 30。代碼可以在 Android 11 設備中創建文件夾“MyFolder”,但不能在 Android 10 設備中創建文件夾。
為什么相同的調用會得到不同的結果?
筆記:
未在 AndroidManifest 中設置 requestLegacyExternalStorage = true; 表示我正在使用范圍存儲。
沒有寫權限。
在 Android 11 設備中,您不需要任何權限即可創建該文件夾。
對於 Android 10 設備,添加 requestLegacyExternalStorage="true" 或使用 Storage Access Framework 或 DownloadManager。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.