简体   繁体   English

如何在 firebase 存储上多次上传可绘制图像?

[英]How to upload multiple time a drawable image on firebase storage?

I would like to know if is it possible to upload multiple time the same image taken from the drawable images on firebase storage in such a way that on the storage the image appears multiple times under different names.我想知道是否可以多次上传从 firebase 存储上的可绘制图像中获取的相同图像,以使图像在存储上以不同的名称多次出现。 What I do is:我要做的是:

imageUri=Uri.parse("android.resource://com.example.project/"+R.drawable.error);
riversRef = storageRef.child("images/" + imageUri.getLastPathSegment()+ "_" + System.currentTimeMillis());
uploadTask = riversRef.putFile(imageUri);

but everytime I use this in the storage does not appear any new images, just the first.但是每次我在存储中使用它时都不会出现任何新图像,只是第一个。 Probably because having the same name on the storage (2131230900) and being the same image the storage ignore it.可能是因为在存储上具有相同的名称 (2131230900) 并且是相同的图像存储忽略它。 The result I want to obtain is that I have the two identical images took from the drawable of my project but in the storage they results as 2 different names我想要获得的结果是我从我的项目的可绘制对象中获取了两个相同的图像,但在存储中它们导致了 2 个不同的名称

Your riversRef is a reference to a specific location/file in Cloud Storage.您的riversRef是对 Cloud Storage 中特定位置/文件的引用。 If you call putFile multiple times on the same reference, you're writing to the same location multiple times, so you'll indeed end up with only one file如果你在同一个引用上多次调用putFile ,你会多次写入同一个位置,所以你最终只会得到一个文件

To write multiple unique files, you'll need to create a separate unique reference for each file.要编写多个唯一文件,您需要为每个文件创建一个单独的唯一引用。 Something like:就像是:

riversRef = storageRef.child("images/" + file.getLastPathSegment() + "_" + System.currentTimeMillis());

By adding the timestamp to the name of the file on Storage here, we end up with unique references if you call this multiple times.通过在此处将时间戳添加到存储上的文件名,如果您多次调用它,我们最终会得到唯一的引用。 Alternatively something like this would also work:或者像这样的东西也可以工作:

for (int i=0; i < 10; i++) {
    riversRef = storageRef.child("images/" + file.getLastPathSegment() + "_" + i);
    uploadTask = riversRef.putFile(file);
}

So now we're just adding a counter to each file name in Cloud Storage.所以现在我们只是为 Cloud Storage 中的每个文件名添加一个计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM