So I was using the open_file
library to open some pdf files. When I switched from cache storage to app’s local document folder storage using getApplicationDocumentsDirectory()
, the files were not opening. Using
var result = await OpenFile.open(filePath);
print(result.message);
return Permission denied: android.permission.MANAGE_EXTERNAL_STORAGE
My filePath is in the format /data/user/0/com.example.<app name>/app_flutter/New-Note-2024-09-19-00-02-48.pdf
It is a bit strange that I need permission to access my app’s local storage
Nevertheless I included
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<application
....
</manifest>
In my AndroidManifest.xml, and here is my flutter code for requesting permissions:
Future<void> requestStoragePermission() async {
if (await Permission.storage.request().isGranted) {
// Permission granted
} else {
openAppSettings();
// Handle permission denied
print("Storage permission denied");
}
}
However it defaults automatically to "Permission denied" without me even doing anything.
And if I go to app settings, it shows "No permissions requested" and outputs No permissions found in manifest for: []15
.
I even tried including READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
on my manifest, still doesn’t work.
2
Answers
I finally found my solution, the issue was using
getApplicationDocumentsDirectory()
, should've usedgetExternalStorageDirectory()
.This should've occurred to me naturally as by using application's local documents directory to save files I was trying to access my App's local files from another external app when running
Openfile.open()
, which would have not worked. Just like how you can't browse an app's local directory from a File Manager (unless rooted).Storage Permission in Android Before 13 and After 13 are changed.
You provide different way in both case while asking for storage permission.
I was already give detailing answer with code.
You can see HERE storage permission solution