如何将图像上传到Firebase存储中的文件夹

时间:2019-08-09 18:19:43

标签: firebase flutter

我正在编写一个从图库中选择图像并将其上传到Firestore中的文件夹中的应用程序。

zip

图像未上传到文件夹中。但是它是直接上传到Firestore中的

2 个答案:

答案 0 :(得分:0)

您必须在.child()中指定文件夹名称。像:.child("/$folder").child("$fileName")

示例:

    StorageReference ref = FirebaseStorage.instance.ref().child('/$folder').child(fileName);
    StorageUploadTask uploadTask = ref.putData(imageData);
    print('Upload triggered in the background.');
    ...

    return await (await uploadTask.onComplete).ref.getDownloadURL();

}

答案 1 :(得分:0)

这是将具有名称的图像上传到Firebase的代码。 Sliderutils是一个具有getName()和getImageUrl()函数(getter和setter)的类。

StorageReference Ref= 
mStorageRef.child(System.currentTimeMillis()+"."+getPath(filePath));

        mStorageTask = Ref.putFile(filePath)
            .addOnSuccessListener(new 
OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Toast.makeText(MainActivity.this, "Image uploaded successfully.", Toast.LENGTH_LONG).show();

                        SliderUtils sliderUtils = new SliderUtils(EditTextName.getText().toString().trim(),
                                taskSnapshot.getDownloadUrl().toString());
                        String uploadId = mDatabaseRef.push().getKey();
                        mDatabaseRef.child(uploadId).setValue(sliderUtils);
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Toast.makeText(MainActivity.this,"Image upload failed.",Toast.LENGTH_SHORT).show();
                }
            })