skip to Main Content

I built an app to upload attachments to Firebase Storage, with the URL saved in Firestore. The upload part works nicely. I also added a download button for downloading the relevant attachment. When clicking the download button, the app should split the last ‘.’ to identify the file type and download the relevant file path. However, unfortunately, when clicking the download button, an error occurs.

error
enter image description here

UI screenshot

enter image description here

A example for the file firestore url

https://firebasestorage.googleapis.com/v0/b/petpulz-93d6f.appspot.com/o/records%2F49785b2d-5bc2-47c1-9ac2-2654627851de3518522212366212285.jpg?alt=media&token=4690a174-c094-4019-a540-17ca8437a69e.jpg

My code

IconButton(
  icon: Icon(Icons.download),
  onPressed: () async {
    // Get the file from _selectedFiles
    final file = _selectedFiles[index];

    // Open the download dialog
    final directory = await getExternalStorageDirectory();
    final savePath = path.join(directory!.path, 'Download', file.path.split('/').last);
    await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('Download'),
        content: Text('Do you want to download ${file.path.split('/').last}'),
        actions: [
          TextButton(
            child: Text('Cancel'),
            onPressed: () => Navigator.pop(context),
          ),
          TextButton(
            child: Text('Download'),
            onPressed: () async {
              // Download the file
              await http.get(Uri.parse(file.path)).then((response) async {
                final bytes = response.bodyBytes;
                await File(savePath).writeAsBytes(bytes);
              });

              // Close the dialog
              Navigator.pop(context);

              // Show a message to indicate that the download is complete
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text('Download complete')),
              );
            },
          ),
        ],
      ),
    );
  },

),

path provider package

  path_provider: ^2.0.2

how to solve this error ?

** AndroidManifest.xml**
enter image description here

print savepath screenshot
enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I solved the error and now file download working for android and ios prefectly.

      IconButton(
                                                    icon: Icon(Icons.download),
                                                    onPressed: () async {
                                                      // Get the file from _selectedFiles
                                                      final file = oneRecord!.fileUrls[index];
    
                                                      // Generate a random file name
                                                      final suffix = file.path.split('.').last.split('?').first;
                                                      final randomName = '${randomString(10)}.$suffix';
    
                                                      // Set the save path based on the platform
                                                      String savePath;
                                                      if (Platform.isIOS) {
                                                        final directory = await getApplicationDocumentsDirectory();
                                                        savePath = '${directory.path}/$randomName';
                                                      } else if (Platform.isAndroid) {
                                                        savePath = '/storage/emulated/0/Download/$randomName';
                                                      } else {
                                                        // Handle other platforms if necessary
                                                        return;
                                                      }
    
                                                      // Open the download dialog
                                                      await showDialog(
                                                        context: context,
                                                        builder: (context) =>
                                                            AlertDialog(
                                                          title: Text('Download'),
                                                          content: Text(
                                                              'Do you want to download $randomName ?'),
                                                          actions: [
                                                            TextButton(
                                                              child: Text('Cancel'),
                                                              onPressed: () =>
                                                                  Navigator.pop(
                                                                      context),
                                                            ),
                                                            TextButton(
                                                              child:
                                                                  Text('Download'),
                                                              onPressed: () async {
                                                                if (await Permission
                                                                    .storage
                                                                    .request()
                                                                    .isGranted) {
                                                                  await http
                                                                      .get(Uri.parse(
                                                                          file
                                                                              .path))
                                                                      .then(
                                                                          (response) async {
                                                                    final bytes =
                                                                        response
                                                                            .bodyBytes;
                                                                    await File(
                                                                            savePath)
                                                                        .writeAsBytes(
                                                                            bytes);
                                                                  });
                                                                }
    
                                                                // Close the dialog
                                                                Navigator.pop(
                                                                    context);
    
                                                                // Show a message to indicate that the download is complete
                                                                ScaffoldMessenger
                                                                        .of(context)
                                                                    .showSnackBar(
                                                                  SnackBar(
                                                                      content: Text(
                                                                          'Download complete')),
                                                                );
                                                              },
                                                            ),
                                                          ],
                                                        ),
                                                      );
                                                    },
                                                  ),
    

    and for the ios path you should import

    import 'package:path_provider/path_provider.dart';
    

    for that import you need to install relevant package

    path_provider: ^2.0.2

    and also specially you need to give permisions in the AndroidManifest.xml file in android and info.plist file in ios folder

    info.plist(ios) add these code line

     <key>LSSupportsOpeningDocumentsInPlace</key>
        <true/>
        <key>UIFileSharingEnabled</key>
        <true/>
    

    simply these permissions are for the documents .

    AndroidManifest.xml(android) add these permission code lines

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

    On Android, the downloaded files should be saved to the download folder, and on iOS, they should be saved to the Files app in a separate folder created using the application name.

    I should clarify that with this code, any file type can be downloaded to the device, not just PDF files and images.


  2. // change Directory and save path like this
    
    final directory = await  Directory('/storage/emulated/0/Download');
    
    final savePath = path.join(directory!.path,file.path.split('/').last);
    

    change directory because an above android 10 version devices not access to use /storage/emulated/0/Android/data directory

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search