簡體   English   中英

在使用 firebase、flutter 和圖像裁剪器將文件上傳到 firebase 之前重命名文件

[英]renaming file before uploading it to firebase using firebase, flutter and image cropper

再會。 我想問一下在上傳到 firebase 存儲之前如何重命名文件(圖像)?

我在選擇和裁剪圖像時使用image_cropper 但是我看不到它是否有任何內置的 function 用於重命名文件。

這是我在本地設備中選擇圖像時的 selectFile。

Future selectFile() async {
    try {
      final image = await ImagePicker().pickImage(source: ImageSource.gallery);
      if (image != null) {
        final croppedImage = await ImageCropper().cropImage(
          sourcePath: image.path,
          aspectRatio: const CropAspectRatio(ratioX: 1, ratioY: 1),
          compressQuality: 100,
          maxWidth: 450,
          maxHeight: 450,
          compressFormat: ImageCompressFormat.jpg,
          aspectRatioPresets: [CropAspectRatioPreset.square],
        );
        setState(() {
          imagePath = croppedImage!.path;
        });
      }
    } on PlatformException catch (e) {
      print(e);
      Navigator.of(context).pop();
    }
  }

這是我的上傳文件 function

Future uploadFile() async {
    final path = 'products/${imagePath.split("/").last}';
    
    final file = File(imagePath);

    final ref = FirebaseStorage.instance.ref().child(path);
    uploadTask = ref.putFile(file);

    final snapshot = await uploadTask!.whenComplete(() {});

    final urlDownload = await snapshot.ref.getDownloadURL();
    var url = urlDownload.toString();
    print(url);

imagePath是我選擇的圖像。

每次我將它上傳到我的 firebase 存儲時,它都會將自己重命名為image_cropper_1234567890.jpg我只想將圖像顯示為1234567890

嘗試這個:

final path = 'products/${imagePath.split("_").last}';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM