-1

I am performing upload data with the image using a firebase real-time database. But the method getDownloadUrl() is giving me an error. I have created a schema named StorageSchema. Actually, I am sending the data by making StorageSchema constructor but when I try to take the Image URL then it gives the error.

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

I want to use getDownloadUrl(), if now not supported due to latest versions then how could I get the image URL.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Orbit
  • 45
  • 7
  • @Hardik Chauhan can u please review this problem. – Orbit Jul 13 '19 at 12:32
  • @Lakhwinder Singh, please I request u to review this. – Orbit Jul 13 '19 at 12:33
  • The `getDownloadUrl()` method returns a task. You need to wait for that task to complete before you can get the actual download URL. See https://stackoverflow.com/questions/51056397/how-to-use-getdownloadurl-in-recent-versions, https://stackoverflow.com/a/37379251 and https://firebase.google.com/docs/storage/android/download-files#download_data_via_url – Frank van Puffelen Jul 13 '19 at 14:24

1 Answers1

1

You should continue with task instead of calling directly 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 
                }
            });

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