When I try to store a file in the main download directory (not the \Android\data\com.example.myApp one) it throws an exception.
PathAccessException: Cannot open file, path = '/storage/emulated/0/Download/MyFile.pdf' (OS Error: Permission denied, errno = 13)
Theres also this message from the Permission-Handler package (which is on version 11.4.0 atm).
D/permissions_handler(21802): No permissions found in manifest for: []22
My Android Manifest:
<manifest xmlns:android=";>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application android:label="@string/app_name" android:name="${applicationName}" android:icon="@mipmap/ic_launcher" android:requestLegacyExternalStorage="true">
...
</application>
</manifest>
My App code:
Future<void> downLoadFile(String fileName) async {
//... getting the response from an API
final bytes = response.bodyBytes;
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
if(!(await Permission.storage.status).isGranted) return;
}
status = await Permission.manageExternalStorage.status;
if (!status.isGranted) {
await Permission.manageExternalStorage.request();
}
String? downloadPath = await getDownloadPath();
if(downloadPath == null) throw Exception("Failed get the download path");
var filePath = "$downloadPath/$fileName";
final bytes = result;
File(filePath).writeAsBytes(bytes);
}
Future<String?> getDownloadPath() async {
Directory? directory;
try {
if (Platform.isIOS) {
directory = await getApplicationDocumentsDirectory();
} else {
directory = Directory("/storage/emulated/0/Download");
if (!await directory.exists()) directory = await getExternalStorageDirectory();
}
} catch (err) {
print("Cannot get download folder path");
}
return directory?.path;
}
I already tried most of the solutions under this Post
When I try to store a file in the main download directory (not the \Android\data\com.example.myApp one) it throws an exception.
PathAccessException: Cannot open file, path = '/storage/emulated/0/Download/MyFile.pdf' (OS Error: Permission denied, errno = 13)
Theres also this message from the Permission-Handler package (which is on version 11.4.0 atm).
D/permissions_handler(21802): No permissions found in manifest for: []22
My Android Manifest:
<manifest xmlns:android="http://schemas.android/apk/res/android">
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application android:label="@string/app_name" android:name="${applicationName}" android:icon="@mipmap/ic_launcher" android:requestLegacyExternalStorage="true">
...
</application>
</manifest>
My App code:
Future<void> downLoadFile(String fileName) async {
//... getting the response from an API
final bytes = response.bodyBytes;
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
if(!(await Permission.storage.status).isGranted) return;
}
status = await Permission.manageExternalStorage.status;
if (!status.isGranted) {
await Permission.manageExternalStorage.request();
}
String? downloadPath = await getDownloadPath();
if(downloadPath == null) throw Exception("Failed get the download path");
var filePath = "$downloadPath/$fileName";
final bytes = result;
File(filePath).writeAsBytes(bytes);
}
Future<String?> getDownloadPath() async {
Directory? directory;
try {
if (Platform.isIOS) {
directory = await getApplicationDocumentsDirectory();
} else {
directory = Directory("/storage/emulated/0/Download");
if (!await directory.exists()) directory = await getExternalStorageDirectory();
}
} catch (err) {
print("Cannot get download folder path");
}
return directory?.path;
}
I already tried most of the solutions under this Post
Share Improve this question edited Apr 2 at 7:33 deceze♦ 523k88 gold badges800 silver badges943 bronze badges asked Mar 24 at 15:10 ShoginShogin 13 bronze badges2 Answers
Reset to default 0in Manifest file
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
and permission for
await Permission.accessMediaLocation.request();
I ended up using the MediaStore API, and that works now.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744244734a4564870.html
评论列表(0条)