Flutter_file_picker: [Android] FilePickerResult.files.bytes returning null

Created on 3 Jan 2021  Â·  1Comment  Â·  Source: miguelpruivo/flutter_file_picker

I am trying to retrieve the bytes of the images I am selecting but I always get null on android, the same code works well on flutter web.

Permission was accepted.

I added these two permissions:

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

file_picker: ^2.1.4

flutter doctor

[✓] Flutter (Channel master, 1.26.0-2.0.pre.137, on Mac OS X 10.15.7 19H2 darwin-x64, locale en)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 12.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.52.0)
[✓] Connected device (3 available)

List<Uint8List> picturesBytes = [];

Future selectPictures() async {

    FilePickerResult result = await FilePicker.platform.pickFiles(
        allowMultiple: true,
        type: FileType.image, // Tried with FileType.custom too, both works on web.
        //allowedExtensions: ["jpg", "png",]
    );

    if(result != null){

      print("Has pictures");

      result.files.forEach((f){

        picturesBytes.add(f.bytes);


      });

      print(picturesBytes);
      print(result.files.length);

    } else {
      print("Null or doesn't have pictures");
    }

  }
new issue

Most helpful comment

Hi, you need to set withData: true if you want to have the file loaded into memory in advance — be careful though, as it might use too much memory if you pick a lot of files, so it’s always a good idea to use it only if needed. Otherwise, just load each file individually on the fly.

On web withData is true by default because there’s no other way to access file blobs with browsers.

>All comments

Hi, you need to set withData: true if you want to have the file loaded into memory in advance — be careful though, as it might use too much memory if you pick a lot of files, so it’s always a good idea to use it only if needed. Otherwise, just load each file individually on the fly.

On web withData is true by default because there’s no other way to access file blobs with browsers.

Was this page helpful?
0 / 5 - 0 ratings