skip to Main Content

Hello i would like to ask, is there a way to resolve this platform exption while picking files from google drive

I/flutter ( 2761): Error while picking file: PlatformException(unknown_path, Failed to retrieve path., null, null)

    class _FilePickerScreenState extends State<FilePickerScreen> {
  File? _pickedFile;

  void _pickFile() async {
    try {
      FilePickerResult? result = await FilePicker.platform.pickFiles();
      if (result != null) {
        setState(() {
          _pickedFile = File(result.files.single.path!);
        });
      }
    } on PlatformException catch (e) {
      print('Error while picking file: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('File Picker Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _pickFile,
              child: Text('Pick File'),
            ),
            SizedBox(height: 20),
            if (_pickedFile != null)
              Text(
                'Selected File: ${_pickedFile!.path.split('/').last}',
                style: TextStyle(fontSize: 16),
              ),
          ],
        ),
      ),
    );
  }
}

2

Answers


  1. String? _pickedFile;
    
    void _pickFile() async {
    try {
      FilePickerResult? result = await FilePicker.platform.pickFiles();
      if (result != null) {
        setState(() {
          _pickedFile = result.files.single.path;
        });
      }
     } on PlatformException catch (e) {
       print('Error while picking file: $e');
     }
    }
    

    Make this change in your code it will work

    Login or Signup to reply.
  2. try this code

    void _pickPhoto() {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(18),
          ),
          title: const Text(
            "Select Picture",
            style: TextStyle(
              color: ColorsHelper.colorBlack,
            ),
          ),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              OutlinedButton(
                style: const ButtonStyle(
                    backgroundColor:
                        MaterialStatePropertyAll(ColorsHelper.colorPrimary)),
                child: const Text(
                  ' Pick from Camera ',
                  style: TextStyle(color: ColorsHelper.colorWhite),
                ),
                onPressed: () async {
                  Navigator.pop(context);
                  selectimage(
                    ImageSource.camera,
                  );
                },
              ),
              OutlinedButton(
                style: const ButtonStyle(
                    backgroundColor:
                        MaterialStatePropertyAll(ColorsHelper.colorPrimary)),
                child: const Text(
                  'Select from Gallery',
                  style: TextStyle(color: ColorsHelper.colorWhite),
                ),
                onPressed: () async {
                  Navigator.pop(context);
                  selectimage(
                    ImageSource.gallery,
                  );
                },
              ),
              const SizedBox(
                height: 2,
                ),
              ],
            ),
          );
        },
      );
    }
    
    
    
     void selectimage(ImageSource source) async {
     XFile? pickedFile = await ImagePicker().pickImage(
      source: source,
     );
     if (pickedFile != null) {
      _imagefile = File(pickedFile.path);
      _cropImage(
        _imagefile!.path,
        );
      }
    }
    
    
      Future<void> _cropImage(String path) async {
      CroppedFile? croppedFile = await ImageCropper().cropImage(
      sourcePath: path,
      aspectRatioPresets: Platform.isAndroid
          ? [
              CropAspectRatioPreset.square,
              CropAspectRatioPreset.ratio3x2,
              CropAspectRatioPreset.original,
              CropAspectRatioPreset.ratio4x3,
              CropAspectRatioPreset.ratio16x9
            ]
          : [
              CropAspectRatioPreset.original,
              CropAspectRatioPreset.square,
              CropAspectRatioPreset.ratio3x2,
              CropAspectRatioPreset.ratio4x3,
              CropAspectRatioPreset.ratio5x3,
              CropAspectRatioPreset.ratio5x4,
              CropAspectRatioPreset.ratio7x5,
              CropAspectRatioPreset.ratio16x9
            ],
      uiSettings: [
        AndroidUiSettings(
          activeControlsWidgetColor: ColorsHelper.colorPrimary,
          toolbarTitle: 'Crop Image',
          toolbarColor: ColorsHelper.colorBlack,
          toolbarWidgetColor: ColorsHelper.colorWhite,
          initAspectRatio: CropAspectRatioPreset.original,
          lockAspectRatio: false,
        ),
        IOSUiSettings(
          title: 'Crop Image',
        ),
      ],
    );
    if (croppedFile != null) {
      setState(() {
        _imagefile = File(croppedFile.path);
        });
      }
    }
    

    added on AndroidMenifest file

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package.name">
    
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission 
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" 
     />
    
      <application
        ...
      </application>
      </manifest>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search