现在我使用以下代码从Storage of 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();
}
});
可以获得图片中显示的URL .. ???
答案 0 :(得分:14)
请点击此链接 - 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
}
});
答案 1 :(得分:4)
在最新的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
}
});
答案 2 :(得分:1)
过去,firebase使用getMetadata().getDownloadUrl()
,今天他们使用getDownloadUrl()
应该这样使用:
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String image = taskSnapshot.getDownloadUrl().toString());
}
});
答案 3 :(得分:1)
根据最新的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();
}
});
}
答案 4 :(得分:0)
不建议使用上述方法taskSnapshot.getMetadata().getDownloadUrl();
,并且将substitute作为替代方法:
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
// ...
}
}
});
答案 5 :(得分:0)
这就是我在Kotlin android中获得下载链接的方式。
ref.putFile(filePath!!)
.addOnSuccessListener {
val result = it.metadata!!.reference!!.downloadUrl;
result.addOnSuccessListener {
var imageLink = it.toString()
}
}
答案 6 :(得分:0)
//kotlin
var uri:Uri
uploadTask.addOnSuccessListener {t ->
t.metadata!!.reference!!.downloadUrl.addOnCompleteListener{task ->
uri = task.result!!
}
}
答案 7 :(得分:0)
是可能的! 我的一些捷径技巧不是从一些令人毛骨悚然的复杂代码行,而是从Kotlin的Firebase存储中获取下载URL
注意 :基于最新的Firebase版本,没有 getdownloadurl()或 getresult()方法(它们是这次贬值的牺牲品)
因此,我在这里使用的技巧是通过从 taskSnapshot对象调用 UploadSessionUri ,依次返回下载网址和上载类型令牌(一个仅在较短的时间范围内可用)和其他一些东西。
由于我们仅需要下载URL,因此我们可以使用子字符串获取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()
}
}
希望有帮助!。
答案 8 :(得分: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();
}
});
答案 9 :(得分:0)
这应该是科特林应该做的事情 您需要添加onCompleteListener
putFile(file).addOnSuccessListener {
it.storage.downloadUrl.addOnCompleteListener {
//then you can call it.result.toString()
}
}
答案 10 :(得分:0)
我尝试了此操作,并获得了在imageview上进行查看的链接:
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.toString();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
答案 11 :(得分:0)
如果您已经具有基于URL的下载基础结构,或者只想共享一个URL,则可以通过在存储引用上调用getDownloadUrl()方法来获取文件的下载URL。
// Create a storage reference from our app
val storageRef = storage.reference
storageRef.child("users/me/profile.png").downloadUrl.addOnSuccessListener {
// Got the download URL for 'users/me/profile.png'
}.addOnFailureListener {
// Handle any errors
}