0

i've put down some code to upload image to my firebase storage and then retrive the url to use it to show the image as a profile pic for my application android.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 2 && resultCode == RESULT_OK){
        Uri uri = data.getData();
        StorageReference filepath = mStorage.child("Photos").child(uri.getLastPathSegment());

        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {



                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                Map<String, Object> data = new HashMap<>();
                data.put("ProfilePic", filepath.toString());


                db.collection("Accounts").document(user.getUid())
                        .set(data, SetOptions.merge())
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Log.d(TAG, "DocumentSnapshot successfully written!");
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.w(TAG, "Error writing document", e);
                            }
                        });


            }
        });
    }
}

with the filepath variable, i use it to save the url of my image into the database inside the account folder. So every pic has a unique profile reference.

The problem is the return value of filepath. It shows the path inside firebase for example

gs://eyesee-gd23b.appspot.com/image/44405857

so i try to use

filepath.getDownloadUrl()

but instead of the url that i can see it on firebase webpage, i retrieve something like

com.google.android.gms.tasks.zzu@dsd873

any suggest?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
mosenco
  • 83
  • 6

1 Answers1

0

Firebase manual states this:

getDownloadUrl() : Task<Uri> : Asynchronously retrieves a long lived download URL with a revokable token.

Key word is asynchronously. You get it in much the same way as you get result from filepath.putFile(uri), by setting an onSuccessListener or onCompleteListener. Or loop filepath.getDownloadUrl().getResult() until it returns a result, but that's just not right.

onSuccessListener calls onSuccess(Uri result), which you override and do with result as you wish directly. Only called if task didn't fail. Otherwise you'd want an onFailureListener too.

onCompleteListener calls onComplete(Task<Uri> thisTask) which you can check for failure and get result with thistask.getResult().

IcedLance
  • 406
  • 3
  • 12