我试图将图像存储到Firebase存储中,然后从Firebase存储中下载那些图像的URI,然后使用foreach循环再次将这些URI上传到Firebase Firestore中。图像已成功上传到Firebase存储中,但是只有最后一个图像的Uri进入了Firestore,前三个失败。我创建了位图的数组列表,然后在其上使用foreach循环。
我的代码
private void UploadingImage() {
if (bitmap != null && bitmap2 != null && bitmap3 != null && bitmap4 != null) {
StorageTask arrayUpload;
fuser = FirebaseAuth.getInstance().getCurrentUser();
ProductName = Objects.requireNonNull(Product_Name_EditText.getText()).toString();
CityName = Objects.requireNonNull(CityNameEditText.getText()).toString();
// Bitmap[] bitmaps=new Bitmap[3];
ArrayList<Bitmap> bitmapArrayList = new ArrayList<>();
bitmapArrayList.add(bitmap);
bitmapArrayList.add(bitmap2);
bitmapArrayList.add(bitmap3);
bitmapArrayList.add(bitmap4);
Bitmap bitresized;
for (Bitmap bitUpload : bitmapArrayList)
{
bitresized = Bitmap.createScaledBitmap(bitUpload, 800, 800, true);
ByteArrayOutputStream baosArray = new ByteArrayOutputStream();
bitresized.compress(Bitmap.CompressFormat.JPEG, 70, baosArray);
byte[] uploadbaosarray = baosArray.toByteArray();
i = i + 1;
fileReference = storageReference.child(ProductName).child(i + ProductName + ".jpg");
arrayUpload = fileReference.putBytes(uploadbaosarray);
arrayUpload.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
} else if (task.isSuccessful()) {
Toast.makeText(Upload_New_Product.this, "Uploaded Successfully", Toast.LENGTH_SHORT).show();
//mProgressBar.setVisibility(View.INVISIBLE);
}
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
assert downloadUri != null;
String mUri = downloadUri.toString();
ProductName = Product_Name_EditText.getText().toString();
ProductRef = db.collection("Sellers").document(CityName).collection(Uid).document(ProductName);
HashMap<String, Object> map = new HashMap<>();
map.put("imageURL" + i, mUri);
//reference.updateChildren(map);
ProductRef.set(map, SetOptions.merge());
} else {
Toast.makeText(Upload_New_Product.this, "Failed!", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Upload_New_Product.this, e.getMessage(), Toast.LENGTH_SHORT).show();
//pd.dismiss();
}
});
}
}
}
答案 0 :(得分:1)
由于上载(并获得下载URL)是异步操作,因此for
循环几乎立即完成,并且所有上载在此之后并行进行。这意味着,当您的map.put("imageURL" + i, mUri)
运行时,i
变量将成为其最终值。
要使代码正常工作,您需要为循环中的每次迭代捕获 i
变量。一种简单的方法是将上传图像并将其URL存储到单独的函数中的代码移动,并将i
的值传递给该函数调用。
类似的东西:
public void uploadFileAtIndex(int i) {
fileReference = storageReference.child(ProductName).child(i + ProductName + ".jpg");
arrayUpload = fileReference.putBytes(uploadbaosarray);
arrayUpload.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
} else if (task.isSuccessful()) {
Toast.makeText(Upload_New_Product.this, "Uploaded Successfully", Toast.LENGTH_SHORT).show();
}
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
assert downloadUri != null;
String mUri = downloadUri.toString();
ProductName = Product_Name_EditText.getText().toString();
ProductRef = db.collection("Sellers").document(CityName).collection(Uid).document(ProductName);
HashMap<String, Object> map = new HashMap<>();
map.put("imageURL" + i, mUri);
ProductRef.set(map, SetOptions.merge());
} else {
Toast.makeText(Upload_New_Product.this, "Failed!", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Upload_New_Product.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
然后在循环中使用它:
for (Bitmap bitUpload : bitmapArrayList) {
...
i = i + 1;
uploadFileAtIndex(i);
}
您可能需要将更多的变量传递给uploadFileAtIndex
,而不是我在此处传递的变量,但是将其传递给i
可以解决您目前遇到的问题。