我想获取图像网址,但getDownloadUrl()无法正常工作?

时间:2019-07-13 11:52:28

标签: android firebase-realtime-database firebase-storage

我正在使用Firebase实时数据库对图像进行上传数据。 但是方法getDownloadUrl()给我一个错误。我创建了一个名为StorageSchema的架构。 实际上,我是通过使用StorageSchema构造函数来发送数据的,但是当我尝试获取Image 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。

1 个答案:

答案 0 :(得分:1)

您应该继续执行任务,而不是直接致电nil

12:33:08.190 INFO [ActiveSessionFactory.apply] - Capabilities are: {
  "browserName": "chrome",
  "chromeOptions": {
    "w3c": false
  },
  "cssSelectorsEnabled": true,
  "javascriptEnabled": true,
  "loggingPrefs": {
    "browser": "ALL"
  },
  "nativeEvents": false,
  "rotatable": false,
  "takesScreenshot": false,
  "version": ""
}
12:33:08.190 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
Starting ChromeDriver 75.0.3770.140 (2d9f97485c7b07dc18a74666574f19176731995c-refs/branch-heads/3770@{#1155}) on port 26448
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1563539588.250][SEVERE]: bind() failed: Cannot assign requested address (99)
12:33:10.078 INFO [ProtocolHandshake.createSession] - Detected dialect: OSS
12:33:10.263 INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session c4e2161e818136d450f5a2cba943cd72 (org.openqa.selenium.chrome.ChromeDriverService)
12:33:19.567 INFO [ActiveSessions$1.onStop] - Removing session c4e2161e818136d450f5a2cba943cd72 (org.openqa.selenium.chrome.ChromeDriverService)

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