task.getResult()引发ClassCastException

问题描述 投票:0回答:1

我正在使用此代码来检索存储在Firebase存储器中的图像的Uri,并将此图像检索到用户piture中,这是我的代码:

final  StorageReference fileReference = storageReference.child(System.currentTimeMillis()
                    + "." + getFileExtension(imgUri));

            uploadTask = fileReference.putFile(imgUri);
           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()){

                       Uri downloadUri =  task.getResult();
                       String mUri = downloadUri.toString();

                       reff = FirebaseDatabase.getInstance().getReference().child("Users")
                               .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                       .child("Information");
                       HashMap<String,Object> map = new HashMap<>();
                       map.put("ImageURL",mUri);
                       reff.updateChildren(map);

它向我抛出此错误:java.lang.ClassCastException:com.google.android.gms.tasks.zzu无法转换为android.net.Uri

解决方案是什么?

java android firebase firebase-storage
1个回答
0
投票

更改

Uri downloadUri =  task.getResult();
String mUri = downloadUri.toString(); 

String mUri= task.getResult().getDownloadUrl().toString();

© www.soinside.com 2019 - 2024. All rights reserved.