java - 无法从 Firebase 存储中检索图像的下载 URL(出现异常)[需要紧急帮助]
问题描述
该代码成功地将图像上传到 Firebase 存储,但没有返回下载 URL。我怎样才能解决这个问题?
我得到了这个例外:java.lang.IllegalArgumentException: getDownloadUrl() is not supported at the root of the bucket.
为什么?
private void uploadFile() {
if (mImageUri != null) {
StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
fileReference.putFile(mImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return mStorageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
System.out.println("Upload success: " + downloadUri);
} else {
// Handle failures
// ...
}
}
});
} else {
Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
}
}
解决方案
所以我有同样的问题。我有一个已经使用了两年的代码,但在 Firebase 中似乎发生了一些变化。Bellow 是一个可以工作并解决这个问题的新代码。Ps:它在 kotlin 中,但您可以在参考链接中找到 android 示例,然后根据我的代码进行调整。
首先声明一个全局变量 private lateinit var filePath: Uri
然后将您的图像放入此 uri 文件中。
然后转到您要上传的代码:
mFireBaseStorage = FirebaseStorage.getInstance()
mphotoStorageReference = mFireBaseStorage.reference //storage references
val storageRef = mphotoStorageReference.child("usuarios_img") //path in storage. This is the folder name in storage
val bmp: Bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath)
val baos: ByteArrayOutputStream = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos) //choose compreess rate (100 means no compression)
//get the uri from the bitmap
val tempUri: Uri = getImageUri(this, bmp)
//transform the new compressed bmp in filepath uri
filePath = tempUri //filePath is a global variable Uri
var uploadTask = storageRef.child("the_name_of_the_file).putFile(filePath)
// [START upload_get_download_url]
val ref = storageRef
uploadTask = ref.putFile(filePath)
val urlTask = uploadTask.continueWithTask { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
ref.downloadUrl
}.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
Log.d("test", "worked, this is the url link "+downloadUri)
} else {
// Handle failures
Log.d("test", "error")
}
}
就这样。
参考 https://firebase.google.com/docs/storage/android/upload-files?hl=pt-br
希望它有所帮助,因为我已经失去了几个小时。
推荐阅读
- emulation - 无法在模拟器中创建集合
- node.js - Heroku、NodeJs 和 React 问题:SCRIPT5007:无法获取未定义或空引用的属性“应用”
- excel - excel的vba代码进行搜索,替换不同列上的相应数据
- android - ScrollView 带走了 LinearLayout 约束
- json - solr 中的 delimited_payload_string 字段类型是什么
- javascript - 未捕获的类型错误:不是函数
- sql - GraphQL 和 SQL - 是否可以通过 1 个 SQL JOIN 减少数据库调用?
- c# - 如何在 C# 中将值从组合框添加到数据网格视图
- javascript - 我可以使用构造函数内部第 3 方库的异步调用填充数组吗?
- spring - 为什么 java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava/lang/String;)Ljava/lang/Class 在部署操作中被抛出?