zmxngh2
zmxngh2

Reputation: 89

Flutter getting files from Firebase Storage

I'm developing application that user can post their photos, and I'm working with modify part. I get their images as File type first, store them in Firebase Storage and get download URL and store it in Firestore, and then show them with Image.network(), but in modify page I'm using Imagecropper which requires the path of image, so I have to convert it to File type, not String. Does anyone know how to get files from firebase, not downloadURL??

Upvotes: 0

Views: 3266

Answers (4)

apatrck01
apatrck01

Reputation: 134

I did not find any pretty solution around here so I now decided to create me own handy function. Hope this helps someone :)

  Future<File> downloadToTmpFile({required String ref}) async {
    final dir = await getTemporaryDirectory();
    final filePath = "${dir.path}/${ref.split('/').last}";
    final file = File(filePath);

    final downloadTask = ref.storageRef.writeToFile(file);
    await downloadTask;
    return file;
  }

Note that i have written a String extension:

extension StringExtension on String {
  Reference get storageRef => FirebaseStorage.instance.ref(this);
}

Ensure that you still have to catch exceptions. Or if you want - you can further modify your the function by handling individual TaskSnapshots differently. This is only the minimum functionalety.

Upvotes: 0

Godwin
Godwin

Reputation: 638

This is how I was able to solve this,

 String userImageUrl = "";
 File _imageFile;
uploadToStorage() async {
   
    String imageFileName = DateTime.now().millisecondsSinceEpoch.toString();

    StorageReference storageReference =
        FirebaseStorage.instance.ref().child(imageFileName);

    StorageUploadTask storageUploadTask = storageReference.putFile(_imageFile);

    StorageTaskSnapshot taskSnapshot = await storageUploadTask.onComplete;

    await taskSnapshot.ref.getDownloadURL().then((urlImage) {
      userImageUrl = urlImage;

      print(userImageUrl)
    });
  }

Upvotes: 0

Hossam Tarek
Hossam Tarek

Reputation: 164

If you need to download a file you have stored in cloud storage you could use writeToFile method

you can call the writeToFile method on any storage bucket reference. The location of where the file will be downloaded to is determined by the absolute path of the File instance provided,

here is the code from the Api doc

import 'package:path_provider/path_provider.dart';

Future<void> downloadFileExample() async {
  Directory appDocDir = await getApplicationDocumentsDirectory();
  File downloadToFile = File('${appDocDir.path}/download-logo.png');

  try {
    await firebase_storage.FirebaseStorage.instance
        .ref('uploads/logo.png')
        .writeToFile(downloadToFile);
  } on firebase_core.FirebaseException catch (e) {
    // e.g, e.code == 'canceled'
  }
}

Upvotes: 0

Bibek
Bibek

Reputation: 71

Use the downloadURL to download the file and save it on the device.

You can use this plugin: https://pub.dev/packages/image_downloader

If you don't want to use any plugin, have a look into this: https://stackoverflow.com/a/59356482/11847608

Upvotes: 1

Related Questions