【发布时间】:2022-01-06 18:54:19
【问题描述】:
所以我将图像上传到 firebase 存储,链接将保存在实时 firebase 中
现在我正在使用 for 循环上传图像,我想让上传到存储的图像名称成为 firebase 的推送 ID 给 realTime。
但由于某种原因,图像会在存储中成功保存为不同的推送 id,但是当保存到实时 id 时会是相同的,并且循环会在相同的 ID 内不断更新。 imagePath 是全局变量。
for (uploadCount = 0; uploadCount < ImageList.size(); uploadCount++) {
imagePath = productRef.child(UID).child("images").push().getKey();
Uri IndividualImage = ImageList.get(uploadCount);
StorageReference ImageName = productImagesRef.child(UID).child(imagePath);
//Compress Images
Bitmap bmp = null;
try {
bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), IndividualImage);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
byte[] data = baos.toByteArray();
//End of compressing
//start on uploading compressed
ImageName.putBytes(data).addOnSuccessListener(taskSnapshot -> ImageName.getDownloadUrl()
.addOnSuccessListener(uri -> {
String url = String.valueOf(uri);
StoreLink(url);
}));
}
这是 firebase 实时上传:
private void StoreLink(String url) {
HashMap<String ,Object> hashMap = new HashMap<>();
hashMap.put("image", url);
hashMap.put("id", imagePath);
dialog.dismiss();
assert imagePath != null;
productRef.child(UID).child("images").child(imagePath).updateChildren(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
finish();
}
});
}
【问题讨论】:
标签: android firebase firebase-realtime-database firebase-storage