Cannot resolve taskSnapshot.getDownloadUrl() in android firebase - android

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

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.

How can I get the download URL in String from firebase storage inside "addOnSuccessListener"

This is my approach to upload an image file and getting the download link
UploadTask uploadTask = profileImageRef.putBytes(data);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String fileUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
Toast.makeText(UserProfile.this, "Uploaded Successfully!", Toast.LENGTH_SHORT).show();
}});
When I logged fileUrl it displays a String like this
com.google.android.gms.tasks.zzu#6ee8ba8
Someone please give me a solution to get the download url.
you can use this,
UploadTask uploadTask = profileImageRef.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 profileImageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});

Firebase storage is error during upload to database

i am trying to use firebase storage but i keep getting a fatal error when i upload and want to send it to my database. the upload is successful but i try to get the download url to my database and i get this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.apps.ayodkay.services, PID: 10693
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
at com.apps.ayodkay.services.Profile$9.onComplete(Profile.java:550)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
this is code i used from the documentation in firebase storage
if (mImageUri != null) {
Date date = new Date();
long time = date.getTime();
Uri file = Uri.fromFile(new File("path/to/images/" + time));
final StorageReference fileReference = mStorageRef.child("images/" + file.getLastPathSegment());
mUploadTask = fileReference.putFile(mImageUri);
// Register observers to listen for when the download is done or if it fails
mUploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
// ...
}
});
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 Objects.requireNonNull(task.getException());
}
// Continue with the task to get the download URL
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
String uri = downloadUri.toString();
mUsernameDatabase.child("image").setValue(uri);
Toast.makeText(Profile.this, "upload Done", Toast.LENGTH_SHORT).show();
mprogress.setVisibility(View.GONE);
} else {
Toast.makeText(Profile.this, "Error uploaing", Toast.LENGTH_SHORT).show();
}
}
});
// [END upload_get_download_url]
}
i tried to use another method and it worked perfectly. i used the UserProfileChangeRequest method instend of send it to my database, i set a new user photo url
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 Objects.requireNonNull(task.getException());
}
// Continue with the task to get the download URL
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
FirebaseUser user = mAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setPhotoUri(downloadUri)
.build();
user.updateProfile(profileUpdates).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(Profile.this, "upload Done", Toast.LENGTH_SHORT).show();
mprogress.setVisibility(View.GONE);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
} else {
Toast.makeText(Profile.this, "Error uploaing", Toast.LENGTH_SHORT).show();
}
}
});

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

Firebase new version getDownloadUrl in picasso not working

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

Categories

Resources