0

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??

zmxngh2
  • 49
  • 1
  • 6

3 Answers3

1

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

Bibek
  • 71
  • 3
  • But my purpose is not to download the image to the device, but to convert Image.network to Image.file – zmxngh2 Apr 24 '21 at 07:56
  • Without fetching/downloading the image file to the device, I don't think you will be able to do any kind of operation on it. You have to load it, then do your operation and once you are done, remove the file from the device. – Bibek Apr 24 '21 at 08:35
  • Is it possible to delete images inside flutter? – zmxngh2 Apr 24 '21 at 09:00
  • You can delete files from directory, if you know the path. https://www.codegrepper.com/code-examples/dart/flutter+delete+file – Bibek Apr 24 '21 at 10:02
0

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'
  }
}
Hossam Tarek
  • 164
  • 6
0

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)
    });
  }
Godwin
  • 608
  • 1
  • 9
  • 14