问题
Right now i am fetching image from Storage of Firebase by using below code :
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..???
回答1:
Follow this link -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
}
});
回答2:
In the past the firebase used getMetadata().getDownloadUrl()
, and today they use getDownloadUrl()
It should be used this way:
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String image = taskSnapshot.getDownloadUrl().toString());
}
});
回答3:
As per the latest firebase changes, here is the updated code:
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:
The above method taskSnapshot.getMetadata().getDownloadUrl();
is deprecated and as a substitute provided this alternative:
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:
that's how I'm getting download link in kotlin android.
ref.putFile(filePath!!)
.addOnSuccessListener {
val result = it.metadata!!.reference!!.downloadUrl;
result.addOnSuccessListener {
var imageLink = it.toString()
}
}
回答6:
//kotlin
var uri:Uri
uploadTask.addOnSuccessListener {t ->
t.metadata!!.reference!!.downloadUrl.addOnCompleteListener{task ->
uri = task.result!!
}
}
回答7:
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
Note:Based on the latest firebase release there is no getdownloadurl() or getresult() method (they are the prey of deprecition this time around)
So the the trick i have used here is that by calling UploadSessionUri from the taskSnapshot object which 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.
Since we need only download url we can use substring to get downloadurl and concat it with alt=media in order to view the image.
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 !.
来源:https://stackoverflow.com/questions/40177250/firebase-how-to-get-image-url-from-firebase-storage