Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSystemException: Cannot open file....(OS Error: Permission denied, errno = 13) #313

Closed
prasantco opened this issue Jun 4, 2020 · 38 comments

Comments

@prasantco
Copy link

I cannot select image from gallery. This is my logcat
FileSystemException: Cannot open file, path = '/storage/emulated/0/DCIM/s9camera/Pic_2020_06_04_19_36_15.jpg' (OS Error: Permission denied, errno = 13).
I'm using permission_handler package for handle permission. Storage and Access Media Location permission has already granted by user but the issue is generated every time when pick image or video from external storage. Inside manifest file we have added android:requestLegacyExternalStorage="true"
Suddenly i get issue on the production app , please support us

@mvanbeusekom
Copy link
Member

This is a file system permission issue, not a user consent permission issue (which is what the permission_handler is trying to solve). Please also have a look at issue #145, #190 and #275.

@prasantco
Copy link
Author

I had already gone through issue #145, #190 and #275 but the issue is genearting also,

@mvanbeusekom
Copy link
Member

Like I said, this is not something related to the permission_handler but to the file system and is not something I am familiar with.

@prasantco
Copy link
Author

prasantco commented Jun 4, 2020 via email

@mvanbeusekom
Copy link
Member

The only thing I can think of is to check if the correct permissions are in the AndroidManifest.xml file:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.yyy">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
...
</manifest>

Otherwise I would try and have a look at the phone it self maybe, check if it is the hardware (maybe SD card is protected, formatting error, ...).

@prasantco
Copy link
Author

prasantco commented Jun 4, 2020 via email

@prasantco
Copy link
Author

prasantco commented Jun 5, 2020 via email

@mvanbeusekom
Copy link
Member

mvanbeusekom commented Jun 5, 2020

How is this related to the library? This should be set in your Application right?

I mean I can update the Example app, but this is simply the example and should not effect your application. Please correct me if I am wrong.

The build.gradle part of the Android library code is set to compileSdkVersion 29 (see here)

@mvanbeusekom mvanbeusekom reopened this Jun 5, 2020
@prasantco
Copy link
Author

prasantco commented Jun 5, 2020 via email

@mvanbeusekom
Copy link
Member

@visual-velocity there is not AndroidManifest.xml file, this belongs to your App so there is nothing I can do on the library side. You will need to add it yourself, just like you need to add the <uses-permission /> tags to your AndroidManifest.xml.

Again I can add it to the example app, but it is just an "example" app which will not affect your application.

@prasantco
Copy link
Author

prasantco commented Jun 5, 2020 via email

@kadnan0900
Copy link

i am facing this issue please guide how i resolve this issue.
D/FFmpeg (25774): ffmpeg is ready!
E/FFmpeg (25774): Exception while trying to run: [/data/user/0/appus.tagit/files/ffmpeg, -noautorotate, -i, /data/user/0/appus.tagit/app_flutter/Movies/TagitProof/proof1/1606908315541.mp4, -vcodec, h264, -crf, 28, -movflags, +faststart, -vf, scale=1280:-2, -preset:v, ultrafast, -b:v, 1000k, /storage/emulated/0/Android/data/appus.tagit/files/flutter_video_compress/1606908315541.mp4]
E/FFmpeg (25774): java.io.IOException: Cannot run program "/data/user/0/appus.tagit/files/ffmpeg":
error=13, Permission denied

@whoamisafal
Copy link

**I am also facing this type of error

How to solve this problem?**

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FileSystemException: Directory listing failed, path = '/data/' (OS Error: Permission denied, errno = 13)

@JAICHANGPARK
Copy link

@prasantco

I had same problem about this issue
I used to connect external usb on my real devices
If I try to write file usb storage, occurred permission error

My sdk : 29
permission_handler : 6.1.3
android:requestLegacyExternalStorage="true"  
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>

how to solved this problem ?

@minhdq2809
Copy link

I think problem is android11, the app cannot create a folder in the root storage /storage/emulated/0/.

@moacirjacomin
Copy link

Try to use android:requestLegacyExternalStorage="true" in the application tag not in tag:

@sanba-anass
Copy link

Try to use android:requestLegacyExternalStorage="true" in the application tag not in tag:

NOT WORKING ETHIER

@khursheed33
Copy link

khursheed33 commented Sep 10, 2021

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.appname">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
   <application
        android:label="appName"
        android:requestLegacyExternalStorage="true"
        android:icon="@mipmap/ic_launcher">
</manifest>

Make sure you use the Code in the Right way

@khursheed33
Copy link

Try to use android:requestLegacyExternalStorage="true" in the application tag not in tag:

Woked for me, Thanks

@ashutosh-mulik
Copy link

Try to use android:requestLegacyExternalStorage="true" in the application tag not in tag:

NOT WORKING ETHIER

android:requestLegacyExternalStorage="true" doesn't work in android 11.
To access external storage in android 11 you have to request All files access.

An app can request All files access from the user by doing the following:
Declare the MANAGE_EXTERNAL_STORAGE permission in the manifest.
Use the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page where they can enable the following option for your app: Allow access to manage all files.

For more :
https://developer.android.com/about/versions/11/privacy/storage
https://developer.android.com/training/data-storage/manage-all-files

@StiffFy
Copy link

StiffFy commented Dec 2, 2021

I am also facing this type of error
How to solve this problem??????

@PhilipPurwoko
Copy link

PhilipPurwoko commented Dec 10, 2021

I solved it by use the configuration below for the AndroidManifest.xml

<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACTION_MANAGE_WRITE_SETTINGS" />
...
<application android:label="App" android:icon="@mipmap/launcher_icon" android:requestLegacyExternalStorage="true">

But still got the same issue for Android 10

@RoyMatipa
Copy link

I don't know if this is can help but when requesting for permissions from permission_handler package request using "await Permission.manageExternalStorage.request().isGranted" this will give you full access compared to await Permission.storage.request().isGranted this will only give you partial access to the files.

@RoyalCoder88
Copy link

Hi friends did somebody fix this annoying problem?
I tried a lot and lot of solutions nothing works :(

@ghost
Copy link

ghost commented Jun 23, 2022

also i have this error E/flutter ( 3412): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Images/IMG-20220609-WA0000.jpg' (OS Error: Permission denied, errno = 13)

@MohammadAlieye
Copy link

I also facing this same issue. It is working on my tab android version 7 but not working on mobile android version 11.

Error : FileSystemException: Cannot open file, path = '/storage/emulated/0/VidMate/download/'Sooraj_Dooba_Hain'FULL_VIDEO_SONG|Arijit_singh_Aditi_Singh_Sharma|_T-SERIES(256k).mp3' (OS Error: Permission denied, errno = 13)

Please help me out.

@Viniciusfr123
Copy link

In android 11

"Migrate existing files from a legacy storage location
A directory is considered a legacy storage location if it isn't an app-specific directory or a public shared directory. If your app creates or consumes files in a legacy storage location, we recommend that you migrate your app's files to locations that are accessible with scoped storage and make any necessary app changes to work with files in scoped storage."

More https://developer.android.com/training/data-storage/use-cases#migrate-legacy-storage

@Taira-U
Copy link

Taira-U commented Oct 11, 2022

I don't know if this is can help but when requesting for permissions from permission_handler package request using "await Permission.manageExternalStorage.request().isGranted" this will give you full access compared to await Permission.storage.request().isGranted this will only give you partial access to the files.

I solved, thx!

@hashirawan01
Copy link

i already add three read write and manage externel storage permission but still got the same error
Directory listing failed, path = '/storage/emulated/0/Android/obb' (OS Error: Permission denied, errno = 13)
please help me what can i do it takes my half working week .@mvanbeusekom

@AlejandroSanchez01
Copy link

AlejandroSanchez01 commented Feb 21, 2023

In my case, the problem was that I want to create a file in this path '/storage/emulated/0/Flutter' The folder Flutter that I create was in the root so the application doesn't have permission despite that I put all the necessary commands for the permissions in the file AndroidManifest.xml. So when I changed the path to for example '/storage/emulated/0/Download/Flutter' . Finally i could create the file.

Remember
dir = await FilePicker.platform.getDirectoryPath();
print('Directory path: $dir');
file = File('$dir/file.txt');
try {
await file.create();
print('File saved at: ${file.path}');
} catch (e) {
print('Error saving file: $e');
}

It works for me!!! I hope you too

@masreplay
Copy link

The error happens sometimes because the file already exists and you try to write on it
FIX: Change file Dir or filename

@vl4dis1ove
Copy link

The error happens sometimes because the file already exists and you try to write on it FIX: Change file Dir or filename

You are right, but when I try to save file in the same path exists file must rewrite, isn't it?

@himmatsingh019
Copy link

manageExternalStorage

Thank you so much, was stuck on this issue for days, resolved with this.

Here is the code for reference.

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");
      }

      print(directory?.path);

      if (directory != null) {
        final String fileName =
            'resume${DateTime.now().millisecondsSinceEpoch}.pdf';
        final String savePath = '${directory.path}/$fileName';

        try {
          dio.download(
            resumeUrl,
            savePath,
          );
        } catch (e, s) {
          widget.showErrorToast(
            context: context,
            message: 'Something Went Wrong',
          );
          print(e);
          print(s);
        }
      }

@ManishJyani
Copy link

it works for me some folder and does not work for some other in android 11, i have modified the xml file in android , don't know what to do :(

@CovenantJunior
Copy link

CovenantJunior commented Jul 26, 2024

AndroidManifest.xml File should have these permissions:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:label="AppName"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:requestLegacyExternalStorage="true">

Flutter code for requesting permission:

  Future<void> requestPermissions() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.manageExternalStorage,
      Permission.storage,
    ].request();
    
  if (statuses.containsValue(PermissionStatus.denied)) {
      Fluttertoast.showToast(
          msg: "App may malfunction without granted permissions",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          timeInSecForIosWeb: 1);
    }
  }

You should end up with similar interfaces like this:
perm1
perm2
perm3

Now for the file writing and folder creation:

final directory = await getDownloadsDirectory();
final storage = getDeviceInternalPath(directory);
final dir = Directory('/$storage/Download);
if (!await dir.exists()) {
  await dir.create(recursive: true);
}

var filePath = '${dir.path}/$_fileName';
final file = File(filePath);

await file.writeAsBytes(response.bodyBytes);

Make use of await getExternalStorageDirectory(); (for sdcard if available) or await getDownloadsDirectory(); (a full permission scope withing the internal device) when saving files, writing to, and creating directories on the device. I made use of await getApplicationDocumentsDirectory(); earlier which resulted in the above error.

They all work fine but the app will only have different file/folder access levels on the device:

  • await getApplicationDocumentsDirectory(); will provide a scope starting with /data/user/0/ or in full, something like, /data/user/0/com.author.app/app_flutter which is file-writeable within that scope and for private data too which can be accessible in **Device Storage**/Android/data/(app_package_name)

  • await getDownloadsDirectory(); or await getExternalStorageDirectory(); on the other hand will provide what you need. And the directory will look like, /storage/emulated/0/ for internal storage and /storage/sdcard/ for obviously, sdcard.

The paths are usually longer than these but I only revealed the prefixes. In my case, I wanted to create a folder in the device's internal storage so I did this to get the first 3 paths:

String getDeviceInternalPath(dir) {
  List<String> comp = dir.path.split('/');
  List<String> trunc = comp.sublist(1, 4);
  return trunc.join('/');
}

Since the first string character is a '/'.

Note, the function above will only work for await getExternalStorageDirectory(); (for sdcard if available) and for await getDownloadsDirectory(); (a full permission scope withing the internal device) when writing to and creating directories on the device.

As for await getApplicationDocumentsDirectory(); we can't do anything about that.

I hope this was helpful 💖

@milfun
Copy link

milfun commented Oct 22, 2024

This works for me. AndroidManifest.xml File should add these permissions:
android.permission.MANAGE_EXTERNAL_STORAGE

 and Run app, with the dialog, check All access to manage all file !!

@usman001-qureshi
Copy link

I don't know if this is can help but when requesting for permissions from permission_handler package request using "await Permission.manageExternalStorage.request().isGranted" this will give you full access compared to await Permission.storage.request().isGranted this will only give you partial access to the files.

i resolved thanks

@milfun
Copy link

milfun commented Dec 27, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests