Java 无法检索图像';s从Firebase存储下载URL(获取异常)[需要紧急帮助]
代码成功地将图像上载到Firebase存储,但没有返回下载URL。我怎样才能解决这个问题 我得到了这个异常:Java 无法检索图像';s从Firebase存储下载URL(获取异常)[需要紧急帮助],java,android,firebase,firebase-storage,Java,Android,Firebase,Firebase Storage,代码成功地将图像上载到Firebase存储,但没有返回下载URL。我怎样才能解决这个问题 我得到了这个异常:java.lang.IllegalArgumentException:getDownloadUrl()在bucket的根目录下不受支持。为什么 private void uploadFile() { if (mImageUri != null) { StorageReference fileReference = mStorageRef.child(System.c
java.lang.IllegalArgumentException:getDownloadUrl()在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();
}
}
private void uploadFile(){
if(mimageri!=null){
StorageReference fileReference=mStorageRef.child(System.currentTimeMillis()
+“+getFileExtension(mimageri));
fileReference.putFile(mimageri).continueWithTask(新的Continuation(){
@凌驾
公共任务(@NonNull Task Task)引发异常{
如果(!task.issusccessful()){
抛出task.getException();
}
//继续执行任务以获取下载URL
返回mstoragerf.getDownloadUrl();
}
}).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Uri downloadUri=task.getResult();
System.out.println(“上传成功:+下载URI”);
}否则{
//处理故障
// ...
}
}
});
}否则{
Toast.makeText(这是“未选择文件”,Toast.LENGTH_SHORT).show();
}
}
@可拉伸/列表\u截面\u分隔器\u材料
真的
-
我认为返回下载url时有问题,请尝试以下代码:
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
//change made here
return fileReference.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();
}
}
private void uploadFile(){
if(mimageri!=null){
StorageReference fileReference=mStorageRef.child(System.currentTimeMillis()
+“+getFileExtension(mimageri));
fileReference.putFile(mimageri).continueWithTask(新的Continuation(){
@凌驾
公共任务(@NonNull Task Task)引发异常{
如果(!task.issusccessful()){
抛出task.getException();
}
//继续执行任务以获取下载URL
//这里的零钱
返回fileReference.getDownloadUrl();
}
}).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Uri downloadUri=task.getResult();
System.out.println(“上传成功:+下载URI”);
}否则{
//处理故障
// ...
}
}
});
}否则{
Toast.makeText(这是“未选择文件”,Toast.LENGTH_SHORT).show();
}
}
所以我也有同样的问题。我有一个我已经使用了两年的代码,但是Firebase似乎发生了一些变化。Bellow是一个新的代码,可以工作并解决这个问题。
Ps:它在kotlin中,但你可以在参考链接中使用android示例,只需调整到我的代码即可
首先声明一个全局变量
私有lateinit var文件路径: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")
}
}
就这些
参考文献
希望它能帮上忙,因为我已经花了几个小时。在xml中,您从哪里得到错误?您在哪里查找错误?您想添加什么?使用@Style属性?
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")
}
}