如何将图像上传到firebase并返回URL?

um6iljoc  于 2023-02-13  发布在  其他
关注(0)|答案(1)|浏览(121)

我正在努力做到以下几点:
1.获取图像的Web URL并将其上传到firebase
1.上传映像后,返回映像的firebase URL

Future<String> portrait(String imageinput) async {
  // Add your function code here!

  final response = await http.get(Uri.parse(imageinput));

  final imageBytes = response.bodyBytes;

  final storageRef = FirebaseStorage.instance
      .ref()
      .child('images/${DateTime.now().toString()}.jpg');

  final uploadTask = storageRef.putData(imageBytes);
  await uploadTask;

  final String imageUrl = await storageRef.getDownloadURL();
  return imageUrl;
}
kr98yfug

kr98yfug1#

请按照下面的代码,使用您的要求
首先,遵循sendImage方法,然后遵循storage method,最后遵循chatservice

class StorageMethods {
  static final FirebaseFirestore firestore = FirebaseFirestore.instance;

  Reference _storageReference;

  Future<Map<String, dynamic>> uploadImageToStorage(File imageFile, String fileName) async {
    try {
      _storageReference = FirebaseStorage.instance.ref().child(fileName);
      UploadTask storageUploadTask = _storageReference.putFile(imageFile);
      var url = await (await storageUploadTask.whenComplete(() {})).ref.getDownloadURL();
      var name = _storageReference.name;
      print("image name ======>> $name");
      print("image url ======>> $url");

      return {"url": url, "name": name};

      return {"url": url, "name": name};
    } catch (e) {
      print("uploadImageToStorage exception ======>> $e");
      return null;
    }
  }

  Future<Map<String, dynamic>> uploadImage({
    @required String type,
    @required String fileName,
    @required File file,
    @required String receiverId,
    @required String senderId,
    @required ChatImageUploadProvider imageUploadProvider,
    @required String sender,
    String senderProfile,
    var numberOfMessage,
  }) async {
    // Set some loading value to db and show it to user
    imageUploadProvider.setToLoading();

    // Get url from the image bucket
    Map<String, dynamic> uploadMap = await uploadImageToStorage(file, fileName);

    // Hide loading
    imageUploadProvider.setToIdle();

    chatService.setImageMsg(
      type,
      uploadMap["url"],
      receiverId,
      senderId,
      uploadMap["name"],
      sender,
      senderProfile,
      numberOfMessage,
    );

    return uploadMap;
  }

在chatService方法内部:

Future<void> setImageMsg(String fileType, String url, String receiverId, String senderId, String bucketName,
      String username, String senderProfile, var numberOfMessage) async {
    Message message;

    message = Message.imageMessage(
        message: fileType == "image" ? "Photo Message" : "DOCUMENT",
        receiverId: receiverId,
        senderId: senderId,
        senderProfile: senderProfile,
        fileUrl: url,
        timestamp: Timestamp.now(),
        type: fileType,
        bucketName: bucketName,
        numberOfMessage: numberOfMessage);

    var map = message.toImageMap();

    await _messageCollection.doc(message.senderId).collection(message.receiverId).add(map);

    _messageCollection.doc(message.receiverId).collection(message.senderId).add(map);
  }

这是我的sendImage方法

void sendImage(String fileType, String fileName, String filePath) async {
    try {
      File selectedFile = File(filePath);
      var uploadMap = await _storageMethods.uploadImage(
        type: fileType,
        fileName: fileName,
        senderProfile: _currentUser.profilePhoto,
        file: selectedFile,
        receiverId: widget.receiverId,
        senderId: _currentUser.firebaseId,
        imageUploadProvider: _imageUploadProvider,
        sender: _currentUser.firebaseId,
        numberOfMessage: ++messageCount,
      );

相关问题