大家好,我是android studio和firebase的新手。在我的应用程序中,用户将从移动图库中选择多个图像,并且所选图像将上载到Firebase存储中。我的问题是我无法获取用户已上传且需要存储在Firebase实时数据库中的所有图像的URL。
以下代码用于从图库中获取多个图像:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode,resultCode,data);
if(resultCode ==Activity.RESULT_OK){
switch (requestCode) {
case PICK_CONTACT:
PickContact(data);
break;
case RESULT_LOAD_IMAGE:
PickMultiImage(data);
break;
}
}
else{
Toast.makeText(AddRentActivity.this, "Failed" , Toast.LENGTH_LONG).show();
}
}
@SuppressLint("SetTextI18n")
private void PickMultiImage(Intent data) {
if (data.getClipData() != null ){
int countClipData = data.getClipData().getItemCount();
int currentImageSelect = 0;
while(currentImageSelect < countClipData){
ImageUri = data.getClipData().getItemAt(currentImageSelect).getUri();
ImageList.add(ImageUri);
currentImageSelect = currentImageSelect +1;
}
textViewImageInfoList.setVisibility(View.VISIBLE);
textViewImageInfoList.setText("You Have Selected " + ImageList.size() + " Images");
}
//Toast.makeText(AddRentActivity.this, "Selected Multiple File" , Toast.LENGTH_LONG).show();
else {
Toast.makeText(AddRentActivity.this, "Select Multiple Image File" , Toast.LENGTH_LONG).show();
}
}
这是将图像上传到存储并获取图像的URL
public void UploadingTheLink() {
ImageFolder = FirebaseStorage.getInstance().getReference()
.child("ImagesFolder").child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid());
for (upload_count = 0; upload_count < ImageList.size(); upload_count++) {
IndividualImage = ImageList.get(upload_count);
ImageName = ImageFolder.child("Image" + IndividualImage.getLastPathSegment());
ImageName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ImageName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
ImagesLink.add(uri.toString());
FirebaseDatabase.getInstance().getReference("Images Uploads")
.child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid())
.child("PICS").push().setValue(ImagesLink);
progressDialog.dismiss();
}
});
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
//calculating progress percentage
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//displaying percentage in progress dialog
progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
}
});
Toast.makeText(AddRentActivity.this, IndividualImage.toString(), Toast.LENGTH_LONG).show();
//Toast.makeText(AddRentActivity.this, "Complete Uploading", Toast.LENGTH_LONG).show();
}
}