android - 我想获取图片网址但 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。
解决方案
您应该继续执行任务而不是直接调用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
推荐阅读
- r - 在 R 中快速多次应用一个函数
- react-native - undefined 不是一个对象(评估 '_reactNativeRecordScreen.RecordScreen.StartRecording'
- r - 将多列堆叠成R中具有单个值的单列
- android - 无法从 Firebase 存储中获取下载 URL(所需参考中不存在对象。)
- google-bigquery - 每个大小为 n 的整数的 BigQuery 分区表
- django - 使用 Apache 和 mod_wsgi 的 Django 网站非常慢
- arrays - 如何在int数组中找到所有和
- optimization - 放松线性约束?
- python - 如何增加 BoxLayout --kivy 内的 mdtextfield 的大小
- r - data.table fcase 与 dplyr case_when