How to fetch image url from firebase Data Storage? - android

I am using this code below and all I get instead of URL is com.google.android.gms.tasks.zzu#a0a540. How can I get the actual URL?
filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
mImageStorage.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Uri downloadUri = filePath.getMetadata().getDownloadUrl();
generatedFilePath = downloadUri.toString();
String downlaodURL = uri.toString();
(mUserDatabase.child("image").setValue(downlaodURL).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
mUploadImage.dismiss();
Toast.makeText(SettingsActivity.this,"image uploaded",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(SettingsActivity.this,"image URL not set",Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
else{
mUploadImage.hide();
Toast.makeText(SettingsActivity.this,"Error uploading image",Toast.LENGTH_SHORT).show();
}
}
});

Related

How to Upload and Retrieve image in latest firebase storage after update

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.

Cant Retrieve Firabase Storage image URL

In my app user can upload their image in Firebase storage and database via my app, but i cant retrieve URL of uploaded image to set their profile Image
private FirebaseAuth mAuth;
private DatabaseReference databaseReference;
private StorageReference UserProfileImageRef;
String currentUserID;
databaseReference=FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
UserProfileImageRef=FirebaseStorage.getInstance().getReference().child("Profile Images");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
String q=UserProfileImageRef.getDownloadUrl().toString();
Toast.makeText(SetupActivity.this, "url"+q, Toast.LENGTH_SHORT).show();
Glide.with(SetupActivity.this)
.load(q)
.into(ProfileImage);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
enter image description hereenter code here
databaseReference=FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID).child("profileimage");
You forgot to add the child "profileimage"
UPDATE:
Your download url is not same as in the Firebase Storage. I assumed you take the wrong download url.
Refer to this link: https://stackoverflow.com/a/50572357/9346054
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());
//You store the download in database.
//set value databaseReference=FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID).child("profileimage").setValue(uri.toString()).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
//Success store the link in the database.
Toast.makeText( getApplicationContext(), "Success",Toast.LENGTH_SHORT ).show();
}
}
});
}
});
}
});
UPDATE BASE ON UR ANSWER:
try this one
Uri resulturi = result.getUri();
final StorageReference filepath = UserProfileImageRef.child(currentUserID + ".jpg");
filepath.putFile(resulturi).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
//Okay part ni dia ambik link kat firebase storage akan pergi ke photoUri kat student tuu
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//Update in database
databaseReference.child("profileimage").setValue(String.valueOf(uri));
Toast.makeText(SetupActivity.this, "successfully", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});
}
}
});
Uri resulturi=result.getUri();
StorageReference filepath=UserProfileImageRef .child(currentUserID +".jpg");
filepath.putFile(resulturi).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task)
{
if (task.isSuccessful())
{
Intent selfIntent=new Intent(SetupActivity.this,SetupActivity.class);
startActivity(selfIntent);
Toast.makeText(SetupActivity.this, "success", Toast.LENGTH_SHORT).show();
final String downloadurl =task.getResult().getStorage().getDownloadUrl().toString();
databaseReference.child("profileimage").setValue(downloadurl)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(SetupActivity.this, "successfully", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
else
{
String message=task.getException().getMessage();
Toast.makeText(SetupActivity.this, "error"+message, Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
}
});
}

Retrieving images from Firebase does not work?

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
}
}
});

How to resolve method getDownloadUrl() [duplicate]

This question already has answers here:
How to use getdownloadurl in recent versions?
(5 answers)
Closed 4 years ago.
Uri resultUri = result.getUri();
String current_user_id= mCurrentUser.getUid();
StorageReference filepath = mImageStorage.child("profile_image").child(current_user_id+".jpg");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
String download_url = task.getResult().getDownloadUrl().toString();
mUserDatabase.child("image").setValue(download_url).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
mProgressDialog.dismiss();
Toast.makeText(SettingsActivity.this,"Success upploading.",Toast.LENGTH_LONG).show();
}
}
});
}else {
Toast.makeText(SettingsActivity.this,"error on upploading.",Toast.LENGTH_LONG).show(); }
mProgressDialog.dismiss();
}
});
Try this:
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//Do what you need to do with the URL
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
You must add addOnSuccessListener() after getDownloadUrl() then get the url string inside onSuccess() method.

Cannot resolve taskSnapshot.getDownloadUrl() in android firebase

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
}
}
});

Categories

Resources