Java 在 Android Firebase Storage 中上传文件后如何获取文件下载 URL?getDownloadUrl() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50570893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
After upload a file in Android Firebase Storage how get the file download Url? getDownloadUrl() not working
提问by Hasib Akter
In my new android firebase project, I used com.google.firebase:firebase-storage:16.0.1
library.
在我的新 android firebase 项目中,我使用了com.google.firebase:firebase-storage:16.0.1
library。
I get the following Error:
我收到以下错误:
I opened another project that had library firebase-storage:15.0.2
and taskSnapshot.getDownloadUrl();
which worked on that project. but after using latest dependency library it's not working.
我打开了一个项目,有库firebase-storage:15.0.2
和taskSnapshot.getDownloadUrl();
其合作上项目。但是在使用最新的依赖库后它不起作用。
Now, how can I get the file URL?
现在,如何获取文件 URL?
Any way to get the file download link?
有什么办法可以得到文件下载链接?
采纳答案by Hasib Akter
I had Found 2 solution for my issue.
我为我的问题找到了 2 个解决方案。
Firebase Google Documentation:
//add file on Firebase and got Download Link
filePath.putFile(imageUri).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();
}
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()){
Uri downUri = task.getResult();
Log.d(TAG, "onComplete: Url: "+ downUri.toString());
}
}
});
Another solution!
另一个解决方案!
It's more easy and smallthan google Firebase documentation and I'll use it:
它比 google Firebase 文档更简单、更小,我将使用它:
filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Log.d(TAG, "onSuccess: uri= "+ uri.toString());
}
});
}
});
回答by goblin
That method has been deprecated on version 16.0.1 (check Firebase release notes) so you have to use
该方法已在 16.0.1 版中弃用(请查看Firebase 发行说明),因此您必须使用
StorageReference.getDownloadUrl()
StorageReference.getDownloadUrl()
If you want to get them after uploading the file, then you must check their documentation here. It is already updated.
如果您想在上传文件后获取它们,那么您必须在此处查看它们的文档。它已经更新了。
回答by ali sampson
taskSnapshot.getDownloadUrl() is deprecated so i recommend that in your addOnSuccessListener() method, you use your storageReference and call the getDownloadUrl() method in order to get the url of the file and you can do whatever you want with it. Hope it helps.
taskSnapshot.getDownloadUrl() 已被弃用,因此我建议在您的 addOnSuccessListener() 方法中,您使用您的 storageReference 并调用 getDownloadUrl() 方法以获取文件的 url,您可以使用它做任何您想做的事情。希望能帮助到你。
mUploadTask = storageRef.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// get the image Url of the file uploaded
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// getting image uri and converting into string
Uri downloadUrl = uri;
fileUrl = downloadUrl.toString();
}
});
}
});
回答by Atif AbbAsi
that's how I'm getting download link in kotlin android.
这就是我在 kotlin android 中获取下载链接的方式。
ref.putFile(filePath!!)
.addOnSuccessListener {
val result = it.metadata!!.reference!!.downloadUrl;
result.addOnSuccessListener {
val imageLink = it.toString()
}
}
回答by Najaf Ali
Firebase upadated their method so kindly update yourself use this method kindly:
Firebase 更新了他们的方法,所以请善意地更新自己,请使用此方法:
this is the basic line at which you feeling annoying, very simple justget the download path in this way
这是您感到烦人的基本行,非常简单,只需以这种方式获取下载路径
StorageReference ref = storageReference.child("images/"+ UUID.randomUUID().toString());
UploadTask uploadTask = ref.putFile(filePath);
Task<Uri> urlTask = uploadTask.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 ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
///here is your image url enjoy this
Toast.makeText(CloudStorageActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
}
} else {
// Handle failures
// ...
}
}
});