Search code examples
androidflutterdartfilepicker

How to select any file/file path in android - Flutter


I have an app, and I need to choose files like PDF JSON, etc. First, I export my file, and then I can import it. To do this, I use the file_picker package with the below codes. Now the question is, I can pick any file's path in iOS, but on the Android side, I can not select the file or file path directly, even though I give permission in the AndroidManifest.

in iOS, when I trigger pickFile() function from the file_picker, it works as well. In the Android section, when I trigger the function, it opens the files, but I can not select the path or file directly. Even I can not see Flutter's application folder.

Here are the codes that I use:

 final result = await FilePicker.platform.pickFiles(
                type: FileType.custom,
                allowedExtensions: ['json'],
              );

Here, after getting the result I check the other conditions. Right now problem is, as I said, I can not get the file/file path from the android side specifically.

Android Manifest section:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />

Solution

  • To limit broad access to shared storage, target Android 11 (API level 30) or higher, request all-files access through the MANAGE_EXTERNAL_STORAGE permission.

    To do that after adding WRITE_EXTERNAL_STORAGE to AndroidManifest.xml, add permission_handler package to your apps and Request permission like:

     void requestPermission() {  
    
    await Permission.manageExternalStorage.request();
      
      }