在firebase存储中创建嵌套文件夹

时间:2017-08-19 17:34:32

标签: android firebase firebase-storage

我的班级中有一个FirebaseDatabase引用和两个FirebaseStorage引用。 第一个StorageReference是ProductImages,第二个引用将是我将从DatabaseReference获取的推送键。 但问题是,当我在存储中上传图像时,不会创建第二个引用。所有图像都存储在ProductImages参考中。 我的代码有问题吗? 这是Firebase的限制吗? 或者还有其他方法可以通过编程方式在Firebase存储中创建嵌套文件夹吗?

我附上了代码:

private DatabaseReference productRef;
private StorageReference productImagesRef, imageRef;

productRef =   FirebaseDatabase.getInstance.getReference().child("Products");
productImagesRef = FirebaseStorage.getInstance().getReference().child("ProductImages");

final String key = productRef.push().getKey();
imageRef = FirebaseStorage.getInstance().getReference().child("ProductImages").child(key);

imageRef.putFile(mainImageUri)
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Handle unsuccessful uploads
                    progressDialog.dismiss();
                    Toast.makeText(getActivity(), "Failed to upload!!! Try Again...", Toast.LENGTH_SHORT).show();
                    return;
                }
            })
            .addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    // When the image has successfully uploaded, we get its download URL
                   mainImageUrl = taskSnapshot.getDownloadUrl();
                }
            });

    imageRef.putFile(sideImageOneUri)
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Handle unsuccessful uploads
                    progressDialog.dismiss();
                    Toast.makeText(getActivity(), "Failed to upload!!! Try Again...", Toast.LENGTH_SHORT).show();
                    return;
                }
            })
            .addOnSuccessListener( getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    // When the image has successfully uploaded, we get its download URL
                    sideImageOneUrl = taskSnapshot.getDownloadUrl();
                }
            });

2 个答案:

答案 0 :(得分:0)

试试这个:

productImagesRef.child(key).putFile(mainImageUri).....
productRef.child(key).putFile(sideImageOneUri)....

希望这会有所帮助。

答案 1 :(得分:0)

好吧,伙计们,我想我已经得到了一个可以接受的答案。我为我存储的不同图像创建了不同的参考。代码如下:

private DatabaseReference productRef;
private StorageReference productImagesRef, imageRef, mainImgRef, sideImgRef;

productRef =   FirebaseDatabase.getInstance.getReference().child("Products");
productImagesRef = FirebaseStorage.getInstance().getReference().child("ProductImages");

final String key = productRef.push().getKey();
imageRef = FirebaseStorage.getInstance().getReference().child("ProductImages").child(key);

mainImgRef = imageRef.child(mainImageUri.getLastPathSegment());
sideImgRef = imageRef.child(sideImageOneUri.getLastPathSegment());


mainImgRef.putFile(mainImageUri)
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
                progressDialog.dismiss();
                Toast.makeText(getActivity(), "Failed to upload!!! Try Again...", Toast.LENGTH_SHORT).show();
                return;
            }
        })
        .addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // When the image has successfully uploaded, we get its download URL
               mainImageUrl = taskSnapshot.getDownloadUrl();
            }
        });

sideImgRef.putFile(sideImageOneUri)
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
                progressDialog.dismiss();
                Toast.makeText(getActivity(), "Failed to upload!!! Try Again...", Toast.LENGTH_SHORT).show();
                return;
            }
        })
        .addOnSuccessListener( getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // When the image has successfully uploaded, we get its download URL
                sideImageOneUrl = taskSnapshot.getDownloadUrl();
            }
        });