0

Any idea on how to return a second value in this code?

private void uploadImage_10() {
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setMessage("Posting");
    pd.show();
    if (mImageUri != null&&mImageUri2 != null){
        final StorageReference fileReference = storageRef.child(System.currentTimeMillis()
                + "." + getFileExtension(mImageUri));
        final StorageReference fileReference2 = storageRef.child(System.currentTimeMillis()
                + "." + getFileExtension(mImageUri2));

        uploadTask2 = fileReference2.putFile(mImageUri2);
        uploadTask = fileReference.putFile(mImageUri);
        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();
                }
                return fileReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    miUrlOk = downloadUri.toString();
                    Uri downloadUri2 = task.getResult();
                    miUrlOk2 = downloadUri2.toString();

                    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");

                    String postid = reference.push().getKey();

                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("postid", postid);
                        hashMap.put("postimage", miUrlOk);
                        hashMap.put("postimage2", miUrlOk2);
                        hashMap.put("description", description.getText().toString());
                        hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());

I want to return fileReference.getDownloadUrl(); and fileReference2.getDownloadUrl(); but have no idea how.

Constantin Beer
  • 5,447
  • 6
  • 25
  • 43
remcxak
  • 3
  • 3

1 Answers1

0

You can't return two values in a single method unless you are passing them in List.

The current requirement can be achieved by passing StorageReference in separate method like below.

private void continueWithTask(StorageReference  reference){
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();
                }
                return reference;
            }
        }

Later call them like this continueWithTask(fileReference) & continueWithTask(fileReference2 )

  • There are other options besides using a list. You can use another collection type, or an array, or a custom object, or a `Map.Entry`, etc.. – Mark Rotteveel Sep 05 '19 at 17:51