【问题标题】:task.getResult() is throwing a java.lang.ClassCastExceptiontask.getResult() 正在抛出 java.lang.ClassCastException
【发布时间】:2019-01-18 04:29:32
【问题描述】:

我正在使用太多开发人员推荐的方法来检索存储在 firebase 存储中的图像的Uri,这是我的代码:

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

它向我抛出了这个错误: com.example.asus.fireapp E/stayinsh: java.lang.ClassCastException: com.google.android.gms.tasks.zzu 无法转换为 android.net.Uri

【问题讨论】:

  • 您正在使用不再存在的 API。
  • 嗨,Doug,我很困惑,我实际上是在使用文档中的 google 代码:firebase.google.com/docs/storage/android/…
  • @joghm 上传任务是否有效?
  • 上传工作正常,是的
  • 顺便说一句,您链接到的示例使用的是 continueWithTask,而不是 continueWith。

标签: android firebase firebase-storage


【解决方案1】:

如果您使用 10.0.1 或更低版本的 FirebaseStorage Api,您的代码应该可以工作 我的意思是 实现 'com.google.firebase:firebase-storage:10.0.1' 或更少

但如果它更大的用途

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

【讨论】:

    【解决方案2】:

    应该是 continueWithTask 而不是 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();
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2020-02-07
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 2016-09-14
      • 2020-08-09
      • 2020-12-22
      • 1970-01-01
      相关资源
      最近更新 更多