Java Firebase 如何从 Firebase 存储中获取图像 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40177250/
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
Firebase how to get Image Url from firebase storage?
提问by Rjz Satvara
Right now i am fetching image from Storage of Firebase by using below code :
现在我正在使用以下代码从 Firebase 存储中获取图像:
mStoreRef.child("photos/" + model.getBase64Image())
.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'photos/profile.png'
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeTextthis, "image not dowloaded", Toast.LENGTH_SHORT).show();
}
});
it is Possible to get this URL which is shown in Image..???
有可能得到这个显示在图像中的 URL..???
采纳答案by Shamik Chodankar
Follow this link -https://firebase.google.com/docs/storage/android/download-files#download_data_via_url
按照此链接 - https://firebase.google.com/docs/storage/android/download-files#download_data_via_url
storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
generatedFilePath = downloadUri.toString(); /// The string(file link) that you need
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
回答by Raphael Ramos
In the past the firebase used getMetadata().getDownloadUrl()
, and today they use getDownloadUrl()
过去使用的 firebase getMetadata().getDownloadUrl()
,今天他们使用getDownloadUrl()
It should be used this way:
应该这样使用:
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String image = taskSnapshot.getDownloadUrl().toString());
}
});
回答by Tupio
The above method taskSnapshot.getMetadata().getDownloadUrl();
is deprecated and as a substituteprovided this alternative:
taskSnapshot.getMetadata().getDownloadUrl();
不推荐使用上述方法,并作为替代提供此替代方法:
final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);
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();
} else {
// Handle failures
// ...
}
}
});
回答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 {
var imageLink = it.toString()
}
}
回答by Ronak Badjatya
As per the latest firebase changes, here is the updated code:
根据最新的 Firebase 更改,以下是更新后的代码:
File file = new File(String.valueOf(imageUri));
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference().child("images");
storageRef.child(file.getName()).putFile(imageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
pd.dismiss();
Toast.makeText(MyProfile.this, "Image Uploaded Successfully", Toast.LENGTH_SHORT).show();
Task<Uri> downloadUri = taskSnapshot.getStorage().getDownloadUrl();
if(downloadUri.isSuccessful()){
String generatedFilePath = downloadUri.getResult().toString();
System.out.println("## Stored path is "+generatedFilePath);
}}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
pd.dismiss();
}
});
}
回答by Togrul
//kotlin
var uri:Uri
uploadTask.addOnSuccessListener {t ->
t.metadata!!.reference!!.downloadUrl.addOnCompleteListener{task ->
uri = task.result!!
}
}
回答by Vishaal S
Yes that's possible !!. Instead of some creepy complicated lines of code here is my shortcut trick to get that downloadurl from firebase storage in kotlin
是的,这是可能的!!这里不是一些令人毛骨悚然的复杂代码行,而是我从 kotlin 中的 firebase 存储中获取下载 URL 的捷径
Note:Based on the latest firebase release there is no getdownloadurl()or getresult()method (they are the prey of deprecition this time around)
注意:基于最新的firebase版本,没有getdownloadurl()或getresult()方法(这次它们是折旧的牺牲品)
So the the trick i have used here is that by calling UploadSessionUrifrom the taskSnapshot objectwhich in turn returns the downloadurl along with upload type,tokenid(one which is available for only shorter span of time) and with some other stuffs.
所以我在这里使用的技巧是,通过从taskSnapshot 对象调用UploadSessionUri,该对象又返回 downloadurl 以及上传类型、tokenid(仅在较短的时间内可用)和其他一些东西。
Since we need only download url we can use substring to get downloadurl and concat it with alt=mediain order to view the image.
由于我们只需要下载 url,我们可以使用 substring 来获取 downloadurl 并使用alt=media 连接它以查看图像。
var du:String?=null
var du1:String?=null
var du3:String="&alt=media"
val storage= FirebaseStorage.getInstance()
var storagRef=storage.getReferenceFromUrl("gs://hola.appspot.com/")
val df= SimpleDateFormat("ddMMyyHHmmss")
val dataobj= Date()
val imagepath=SplitString(myemail!!)+"_"+df.format(dataobj)+".jpg"
val imageRef=storagRef.child("imagesPost/"+imagepath)
val baos= ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos)
val data=baos.toByteArray()
val uploadTask=imageRef.putBytes(data)
uploadTask.addOnFailureListener{
Toast.makeText(applicationContext,"Failed To Upload", Toast.LENGTH_LONG).show()
}.addOnSuccessListener { taskSnapshot ->
imageRef.downloadUrl.addOnCompleteListener (){
du=taskSnapshot.uploadSessionUri.toString()
du1=du!!.substring(0,du!!.indexOf("&uploadType"))
downloadurl=du1+du3
Toast.makeText(applicationContext,"url"+downloadurl, Toast.LENGTH_LONG).show()
}
}
Hope it helps !.
希望能帮助到你 !。
回答by Ram Chhabra
This works for me with dependency - implementation 'com.google.firebase:firebase-storage:19.1.0'
这对我有依赖性 - 实现 'com.google.firebase:firebase-storage:19.1.0'
ref.putFile(iconUriLocalFilePath)
.addOnSuccessListener(
new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(
UploadTask.TaskSnapshot taskSnapshot)
{
Task<Uri> uri = ref.getDownloadUrl();
String iconPathFirebase = uri.getResult().toString();
}
});
回答by Kirill
Thats works fine on latest firebase builds
这在最新的 Firebase 版本上运行良好
UploadTask uploadTask = ref.putBytes(data);
uploadTask.addOnSuccessListener(new OnSuccessListener<TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
taskSnapshot.getStorage().getDownloadUrl().addOnCompleteListener(
new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
String fileLink = task.getResult().toString();
//next work with URL
}
});
回答by freedom chuks
This should be how it should be done in kotlin you need to add an onCompleteListener
这应该是在 kotlin 中应该如何完成的,您需要添加一个 onCompleteListener
putFile(file).addOnSuccessListener {
it.storage.downloadUrl.addOnCompleteListener {
//then you can call it.result.toString()
}
}