I keep get error like this when i run my app in android 10
Failed to ensure /storage/0DFE-330F/Android/data/my_app/files: java.lang.IllegalStateException: Failed to prepare /storage/0DFE-330F/Android/data/my_app/files/: android.os.ServiceSpecificException: (code -13)
PathAccessException: Cannot open file, path = '/storage/emulated/0/Download/abc.jpg' (OS Error: Permission denied, errno = 13)
I have function to download image to the folder ‘download’ in the user device. Here is my code :
PermissionStatus storagePermissionStatus = await Permission.storage.status;
Future.delayed(const Duration(seconds: 1), () async {
Directory? directory;
try {
if (Platform.isAndroid) {
print("PLATFORM - ANDROID");
if (storagePermissionStatus.isGranted) {
print("STATUS - GRANTED");
directory = await getExternalStorageDirectory();
String newPath = "";
List<String> paths = directory!.path.split("/");
for (int x = 1; x < paths.length; x++) {
String folder = paths[x];
if (folder != "Android") {
newPath += "/$folder";
} else {
break;
}
}
newPath = "$newPath/Download";
directory = Directory(newPath);
} else if (storagePermissionStatus.isDenied) {
print("STATUS - DENIED");
// function to open app permission
}
}else {
print("PLATFORM - IOS");
if (await Permission.storage.request().isGranted) {
directory = await getApplicationDocumentsDirectory();
} else {
Navigator.pop(context);
return false;
}
}
print("directory : ${directory?.path}");
if (await directory?.exists() == true || Platform.isIOS) {
String tempName = filename;
int i = 1;
while (await File('${directory?.path}/$tempName.jpg').exists()) {
tempName = '$filename ($i)';
i++;
}
File saveFile = File("${directory?.path}/$tempName.jpg");
print("version : ${PublicVariable.oSDeviceVersions}");
if (Platform.isAndroid && PublicVariable.oSDeviceVersions < 10.0) {
} else {
print("IOS / ANDROID >= 10");
print("savefile : ${saveFile.path}");
saveFile.writeAsBytes(pngBytes).then((_) async {
print("write file");
if (mounted) {
final Directory appDocDir =
await getApplicationDocumentsDirectory();
await Future.delayed(const Duration(seconds: 1), () async {
final File localFile = File('${appDocDir.path}/$tempName.jpg');
localFile.writeAsBytesSync(pngBytes);
closeDownloadLinkService(
context: context,
withDefaultMessager: true,
customSuccessMessage: "",
successDonwload: true);
OpenFilex.open(localFile.path);
});
}
});
}
}
}
When I click download for the first time after install the app, it get “DENIED” and open the app permission then i allow the storage permission like this
But when i click the download again, i get output like this
From the output, the error is when i call writeAsBytes and also before that i get failed when call getExternalStorageDirectory
In the manifest I already add :
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:requestLegacyExternalStorage="true"
I don’t know why the error say it denied and failed to ensured the storage even though the status permission is granted. Also it only happen in Android 10, it can run successfully in android 5, 8, 11. Is there any steps that I missed?