1

I am using what is recommended by too many developers in order to retrieve the Uri of an image stored in the firebase storage , this my code :

final StorageReference filereference = storageReference.child(System.currentTimeMillis()
                    +"."+getFileExtension(imageUri));
uploadTask =filereference.putFile(imageUri);
uploadTask.continueWith(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()){
 try{
     Uri downloadUri = task.getResult();
     String mUri =downloadUri.toString();
     }catch (Exception e){
     Log.e("testingerror",e.toString());
     }

it throws me this error : com.example.asus.fireapp E/stayinsh: java.lang.ClassCastException: com.google.android.gms.tasks.zzu cannot be cast to android.net.Uri

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
joghm
  • 569
  • 3
  • 20

2 Answers2

0

Your code should work if your using a FirebaseStorage Api of 10.0.1 or less what i mean is implementation 'com.google.firebase:firebase-storage:10.0.1' or less

but if its greater use

StorageReference filepath=user_Image_ref.child(user_id+".jpg");
        filepath.putFile(resulturi).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                if(task.isSuccessful()) {
                    String getimageurl = task.getResult().getDownloadUrl().toString();

                    databaseReference.child("PHOTO").setValue(getimageurl);


                }else {
                    Toast.makeText(Register_activity.this,"errr",Toast.LENGTH_LONG).show();

                }
                String getimageurl = task.getResult().getDownloadUrl().toString();

                databaseReference.child("PHOTO").setValue(getimageurl);
            }
        });
Andronicus
  • 25,419
  • 17
  • 47
  • 88
0

It should be continueWithTask instead of continueWith

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();

}
ahmedshahriar
  • 1,053
  • 7
  • 25