My i want to upload a file to the flutter application

19 views Asked by At

I/flutter ( 3041): [MethodChannelFilePicker] Platform exception: PlatformException(read_external_storage_denied, User did not allow reading external storage, null, null)

Future<void> pickFile() async {
    try {
      FilePickerResult? result;
      String? _fileName;
      PlatformFile? pickedFile;
      bool isLoading = false;
      File? fileToDisplay;

      result = await FilePicker.platform.pickFiles(
        type: FileType.media,
        allowMultiple: false,
      );
      if (result != null) {
        _fileName = result!.files.first.name;
        pickedFile = result!.files.first;
        fileToDisplay = File(pickedFile!.path.toString());
        print("file name:$_fileName");
      } else {
        // User canceled the picker
        print('User canceled file picker');
      }
    } catch (e) {
      showSnackBar(context, e.toString());
    }
  }

I am tring to upload file to flutter application but got user did not allowed external storage in application it is not asking for any permission

1

There are 1 answers

0
Mohamed elShazly On

You must request storage permission first. add read external storage permission in android\app\src\main\AndroidManifest.xml

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

and request it by using permission_handler package.

example for the request:

final status = await Permission.storage.status;
final isGranted = status == PermissionStatus.granted;
if(!isGranted) {
  // alert user: storage permission is required.
}else{
  await pickFile();
}