dacscan3669
dacscan3669

Reputation: 761

How to return a variable from a listener method

In the below code, I am declaring class variable downloadUrl which is set later in the method ImageLoad. And i am trying to set the variable and return it when the method is called. But I see that the variable returns null. What is happening here? How do I correctly return this variable?

public class ImageActions {

private Uri downloadUrl;

public Uri ImageLoad()
{
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReferenceFromUrl("gs://apptest.appspot.com");

    Uri file = Uri.fromFile(new File("sdcard/DCIM/Camera/1466831822883.jpg"));

    StorageMetadata metadata = new StorageMetadata.Builder().setContentType("image/jpeg").build();

    UploadTask uploadTask = storageRef.child("images/"+file.getLastPathSegment()).putFile(file, metadata);

    uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
            double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
            System.out.println("Upload is " + progress + "% done");
        }
    }).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
            System.out.println("Upload is paused");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
            System.out.println("Upload failed");
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // Handle successful uploads on complete
            downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
            System.out.println("beforeReturn" + downloadUrl); //This prints correct (non-null) value
            System.out.println("Upload completed");                
        }
    });

    System.out.println("beforeReturn" + downloadUrl); //This prints null

    return downloadUrl;

 }
}

Upvotes: 0

Views: 839

Answers (1)

Ichigo Kurosaki
Ichigo Kurosaki

Reputation: 3843

You can do it as follows:

public class ImageActions {

public interface OntaskCompleted{
   void onSuccess(Uri returnurl);
   void onfail();
}

private Uri downloadUrl;

public void ImageLoad(final OntaskCompleted listener)
{
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReferenceFromUrl("gs://apptest.appspot.com");

    Uri file = Uri.fromFile(new File("sdcard/DCIM/Camera/1466831822883.jpg"));

    StorageMetadata metadata = new StorageMetadata.Builder().setContentType("image/jpeg").build();

    UploadTask uploadTask = storageRef.child("images/"+file.getLastPathSegment()).putFile(file, metadata);

    uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

        }
    }).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
            System.out.println("Upload is paused");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
            listener.onfail();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // Handle successful uploads on complete
            downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
                  System.out.println("Upload completed");  
            listenr.onSuccess(downloadurl)              
        }
    });

 }
}

Now call this function as follows:

ImageLoadObject.ImageLoad(new OntaskCompleted(){

   @Override
   void onSuccess(Uri url){
      //Here you will get the url after upload complete
   }
   @Override
   void onFail(){

   }
});

Hope this helps you.

Upvotes: 2

Related Questions