【发布时间】:2019-11-22 22:14:50
【问题描述】:
我正在使用 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。
【问题讨论】:
-
@Hardik Chauhan 你能复习一下这个问题吗?
-
@Lakhwinder Singh,我请你检查一下。
-
getDownloadUrl()方法返回一个任务。您需要等待该任务完成,然后才能获得实际的下载 URL。请参阅stackoverflow.com/questions/51056397/…、stackoverflow.com/a/37379251 和 firebase.google.com/docs/storage/android/…
标签: android firebase-realtime-database firebase-storage