首页 > 解决方案 > 我想获取图片网址但 getDownloadUrl() 不起作用?

问题描述

我正在使用 firebase 实时数据库对图像执行上传数据。但是 getDownloadUrl() 方法给了我一个错误。我创建了一个名为 StorageSchema 的模式。实际上,我通过创建 StorageSchema 构造函数来发送数据,但是当我尝试获取图像 URL 时,它会给出错误。

private void upload_product() {
 if(mImageUri != null){
  final StorageReference fileReference = 
    mStorageRef.child(System.currentTimeMillis()
        +"."+getFileExtension(mImageUri));

        uploadProgressBar.setVisibility(View.VISIBLE);
        uploadProgressBar.setIndeterminate(true);

        mUploadTask = fileReference.putFile(mImageUri)
         .addOnSuccessListener(new 
           OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
             public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
              Handler handler = new Handler();
              handler.postDelayed(new Runnable() {
              @Override
              public void run() {
               uploadProgressBar.setVisibility(View.VISIBLE);
               uploadProgressBar.setIndeterminate(false);
               uploadProgressBar.setProgress(0);
              }
            },500);
        Toast.makeText(Add_Item.this, "Product Upload Successful", 
         Toast.LENGTH_SHORT).show();
        StorageSchema upload = new StorageSchema(
               productname.getText().toString().trim(),
               productdescription.getText().toString(),

              //the problem is here in getDownloadUrl() method 

               taskSnapshot.getDownloadUrl().toString());
               String uploadId = mDatabaseRef.push().getKey();
               mDatabaseRef.child(uploadId).setValue(upload);
               uploadProgressBar.setVisibility(View.INVISIBLE);
              }
         });
    }
}


//starting the StorageSchema class having the schema
public class StorageSchema {

private String productname;
private String productdescription;
private String imageUrl;
private int rate;
private String unit;
private int position;
private String key;


public StorageSchema(){
    //empty constructor needed
}

public StorageSchema(int position){
    this.position=position;
}

public StorageSchema(String productname, String productdescription, String 
imageUrl, int rate, String unit){
if(productname.trim().equals("")){
    productname = "No Name";
}
this.productname = productname;
this.productdescription = productdescription;
this.imageUrl = imageUrl;
this.rate = rate;
this.unit = unit;
}

我想使用 getDownloadUrl(),如果由于最新版本而现在不支持,那么我该如何获取图像 URL。

标签: androidfirebase-realtime-databasefirebase-storage

解决方案


您应该继续执行任务而不是直接调用getDownloadUrl();

Task<Uri> urlTask = mUploadTask.continueWithTask(task -> {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }

                // Continue with the task to get the download URL
                return fileReference.getDownloadUrl();
            }).addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    if (downloadUri != null) {
                        // HERE IS YOUR DL LINK
                        String dl =  downloadUri.toString();
                    }

                } else {
                       // SOMETHING WENT WRONG 
                }
            });

来源: https ://firebase.google.com/docs/storage/android/upload-files


推荐阅读