How to add image list in Firebase?

Viewed 382

I want to upload image list in firebase but in the firebase upload success listener doesn't add image list on OnSuccessListener.

Tried I add string value outside of the add onSuccessListener class it works and i tried to log the url it works too.But it doesn't add the string value in the arrayList in the OnSuccessListener. Thank you for the help.

Main code

 public void UploadImage() {
    if (isUserSelected != null) {
        progressDialog.setTitle("Image is Uploading...");
        Log.d(TAG, "UploadImage: uploading");
        progressDialog.show();
        Log.d(TAG, "UploadImage:  after progress bar");
        for (int i = 0; i < multipleFileUploads.size(); i++) {
            Log.d(TAG, "UploadImage: " + i);
            final StorageReference storageReference2 = storageReference.child(System.currentTimeMillis() + "." + GetFileExtension(multipleFileUploads.get(i)));
            storageReference2.putFile(multipleFileUploads.get(i))
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            Log.d(TAG, "onSuccess: ");
                            progressDialog.dismiss();
                            Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
                            storageReference2.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    Log.d(TAG, "onSuccess: uri " + uri.toString());
                                    imagelist.add(uri.toString());

                                }
                            });
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d(TAG, "onFailure: " + e.getMessage());
                }
            }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    Log.d(TAG, "onProgress: " + taskSnapshot.getBytesTransferred());
                    Log.d(TAG, "onProgress: " + taskSnapshot.getTotalByteCount());
                }
            }).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    Log.d(TAG, "onComplete: " + task.getException());
                }
            });
        }
        imageList.add("custom list");
        uploadinfo imageUploadInfo = new uploadinfo(txtdata.getText().toString(), txtData1.getText().toString(), imageList.toString());
        String ImageUploadId = databaseReference.push().getKey();
        databaseReference.child(ImageUploadId).setValue(imageUploadInfo);


    } else {
        Toast.makeText(uploadimg1.this, "Please Select Image or Add Image Name", Toast.LENGTH_LONG).show();

    }

POJO Code for Firebase

public class uploadinfo {
public String imageName;
public String imageName1;
public String imageListString;

public uploadinfo(String imageName, String imageName1, String imageListString) {
    this.imageName = imageName;
    this.imageName1 = imageName1;
    this.imageListString = imageListString;
}

public String getImageName() {
    return imageName;
}

public void setImageName(String imageName) {
    this.imageName = imageName;
}

public String getImageName1() {
    return imageName1;
}

public void setImageName1(String imageName1) {
    this.imageName1 = imageName1;
}

public String getImageListString() {
    return imageListString;
}

public void setImageListString(String imageListString) {
    this.imageListString = imageListString;
    }
}

In Firebase enter image description here

2 Answers

The problem might be in your model. In firebase the order goes imageListString, imageName, imageName1. But in model in constructor you got String imageName, String imageName1, String imageListString. At the same time im not sure how you generate the list which you create BEFORE send it to Firebase.

You can try this method

 UploadTask uploadTask = storageReference2.putFile(multipleFileUploads.get(i));   
Task<Uri> urlTask = uploadTask.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();
            }

            // Continue with the task to get the download URL
            return ref.getDownloadUrl();
        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                Uri downloadUri = task.getResult();
                imagelist.add(downloadUri.toString());
            } else {
                // Handle failures
                // ...
            }
        }
    });
Related