I am using what is recommended by too many developers in order to retrieve the Uri of an image stored in the firebase storage , this my code :
final StorageReference filereference = storageReference.child(System.currentTimeMillis()
+"."+getFileExtension(imageUri));
uploadTask =filereference.putFile(imageUri);
uploadTask.continueWith(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 filereference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if(task.isSuccessful()){
try{
Uri downloadUri = task.getResult();
String mUri =downloadUri.toString();
}catch (Exception e){
Log.e("testingerror",e.toString());
}
it throws me this error :
com.example.asus.fireapp E/stayinsh: java.lang.ClassCastException: com.google.android.gms.tasks.zzu cannot be cast to android.net.Uri
Your code should work if your using a FirebaseStorage Api of 10.0.1 or less
what i mean is
implementation 'com.google.firebase:firebase-storage:10.0.1' or less
but if its greater use
StorageReference filepath=user_Image_ref.child(user_id+".jpg");
filepath.putFile(resulturi).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()) {
String getimageurl = task.getResult().getDownloadUrl().toString();
databaseReference.child("PHOTO").setValue(getimageurl);
}else {
Toast.makeText(Register_activity.this,"errr",Toast.LENGTH_LONG).show();
}
String getimageurl = task.getResult().getDownloadUrl().toString();
databaseReference.child("PHOTO").setValue(getimageurl);
}
});
It should be continueWithTask instead of continueWith
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();
}
return filereference.getDownloadUrl();
}
Related
I am trying to upload and retrieve the image and follow all the results available at Stack and tutorials but none of example help me.
how to upload an image on button click in latest firebase storage
I am following this tutorial
https://www.simplifiedcoding.net/firebase-storage-example/
StorageReference reference= storageReference.child(System.currentTimeMillis()+ "."+getFileExtension(filePath));
mUploadTask= reference.putFile(filePath);
Task<Uri> urlTask = mUploadTask.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 storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
You are doing it right to upload the image, after you return the storageReference.getDownloadUrl(), just set that value into your firebase database.
StorageReference reference= storageReference.child(System.currentTimeMillis()+ "."+getFileExtension(filePath));
mUploadTask= reference.putFile(filePath);
Task<Uri> urlTask = mUploadTask.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 storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
mDatabaseRef.child("images").child("imageUrl").setValue(downloadUri.toString());
} else {
// Handle failures
// ...
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
You will need to request that image url String from firebase and after that, to show the image in any ImageView use Picasso or Glide to load it.
In my project, I am able to getDownloadUrl() after uploading a file
ref.putFile(imgUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
if(task.getException() != null) {
throw task.getException();
}
}
return ref.getDownloadUrl();
}
}) ...
However, since I am resizing the image with a cloud function and re-uploading it to the bucket, the download url will change, so the only way to know the location of the image is through a StorageReference. How can I getDownloadUrl() from StorageReference? I've tried ref.getDownloadUrl().addOnSuccessListener which doesn't get fired and ref.getDownloadUrl().addOnCompleteListener and the task.getResult() throws an error.
Try using this :
I was trying to store images
You can get the download URL from task.getResult().toString()
final StorageReference ref = FirebaseStorage.getInstance().getReference().child("images").child(imageName)
uploadTask = ref.putBytes(data);
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();
String URL = task.getResult().toString();
} else {
// Handle failures
// ...
}
}
});
Retrieving images from Firebase does not work?
Hi I am working on upload and retrieve images from Firebase.
But taskSnapshot.getDownlaodUrl is currently deprecated.
So I use the alternative as answered in this question
taskSnapshot.getDownloadUrl() is deprecated
but none of these alternatives work for me.
#Override
public void onActivityResult( int requestCode,int resultcode,Intent data) {
super.onActivityResult(requestCode,resultcode,data);
if(requestCode==GALLERY_INTENT&&resultcode==RESULT_OK)
{ mbar.setVisibility(View.VISIBLE);
Uri uri=data.getData();
final StorageReference fileupload=mStorage.child("Photos").child(uri.getLastPathSegment());
fileupload.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>(){
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mbar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,"Succesfully Uploaded",Toast.LENGTH_SHORT).show();
Task<Uri> firebaseUri = taskSnapshot.getStorage().getDownloadUrl();
Picasso.get().load(firebaseUri.getResult.toString()).into(image);
}
}
);
This is my code for upload and retrive the data.
It gives me error for Task is not yet Complete
and when i try another alternative
filepath.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
String downloadUrl = task.getResult().toString();
Picasso.get().load(downloadUrl).into(image);
}
});
It gives me error of Object does not exit
I am making a child reference for Photos and try to retrive the image.
Can anyone solve whats wrong in this code.
final StorageReference fileupload=mStorage.child("Photos").child(uri.getLastPathSegment());
UploadTask uploadTask = fileupload.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();
}
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Picasso.get().load(downloadUri.toString()).into(image);
} else {
// Handle failures
}
}
});
saveUrlToCategory(categoryIdSelect,taskSnapshot.getDownloadUrl().toString(),textname, textdescription);
getDownlaodUrl is not working. I need to save this in a firebase database and than display it in picasso. How can I get this working?
UploadTask uploadTask = FirebaseStorage.getInstance().getReference().child(user.getUid()).child("avatar").child(avatarName)
.putFile(uri);
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();
}
return FirebaseStorage.getInstance().getReference().child(user.getUid()).child("avatar").child(avatarName).getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
String downloadUrl = downloadUri.toString();
childRef.child("avatar")
.setValue(downloadurl);
// Or in your case it is:
saveUrlToCategory(categoryIdSelect,downloadUrl,textname, textdescription);
} else {
Log.e("FirebaseError:", "Error");
}
}
});
This is my code to upload image to firebase storage. it works well. but I cannot get download url after upload
ref.putFile(uri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
URL url = taskSnapshot.getDownloadUrl()
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
}
});
In firebase 'com.google.firebase:firebase-storage:16.0.1', it cannot resolve getDownloadUrl().
Try this implementation. Use continueWithTask to get the downloaded url.
final UploadTask uploadTask = filepath.putFile(uri);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
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 filepath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
thumb_download_url = task.getResult().toString();
}
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
Task<Uri> urlTask = ref.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 ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
// Downloadable uri
} else {
// Handle failures
}
}
});