Get Image Download url by Passing Firebase Storage Reference [duplicate] - android

This question already has answers here:
How to get URL from Firebase Storage getDownloadURL
(13 answers)
Closed 3 years ago.
Is it Possible to get Download uri by using Firebase Storage Reference ?
This is question not similar to this question
Above question problem was get image url by uploading image.
I want to try something get Image Download URI by providing Image Ref from Storage.
I tried something :
storageReference.child("images/download.jpg").downloadUrl.addOnSuccessListener { uri ->
if (uri != null){
Log.e("url", uri.toString())
}
}
Image :
It shows Error (i know something like that does not exist) :
E/StorageException: { "error": { "code": 404, "message": "Not
Found. Could not get object", "status": "GET_OBJECT" }}
java.io.IOException: { "error": { "code": 404, "message": "Not Found. Could not get object", "status": "GET_OBJECT" }}
at com.google.firebase.storage.network.NetworkRequest.parseResponse(com.google.firebase:firebase-storage##17.0.0:455)
at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(com.google.firebase:firebase-storage##17.0.0:435)
at com.google.firebase.storage.network.NetworkRequest.processResponseStream(com.google.firebase:firebase-storage##17.0.0:426)
at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage##17.0.0:280)
at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage##17.0.0:294)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage##17.0.0:70)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage##17.0.0:62)
at com.google.firebase.storage.GetDownloadUrlTask.run(com.google.firebase:firebase-storage##17.0.0:74)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
I am just looking for show image without using glide library. so i am looking for image uri to show image.

Try this:
StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("storage ref url in string");
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//do your stuff- uri.toString() will give you download URL\\
}
});
or directly use StorageReference if you already initialized it.

Related

Can't get image from firebase storage using glide library

i'm trying to get profile picture for every user from firebase storage within a firestore recycler adapter. i'm using the glide library to load the picture on the imageview. But i'm getting a StorageException. I always used the same code before to get picture from Firebase Storage but now i don't know what does changed.
Here's my code:
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder usersViewHolder, int i, #NonNull User user) {
FirestoreUsage.getUserPictureReference(user.getMail(), user.getGender()).child("profile_picture.jpg").getDownloadUrl()
.addOnSuccessListener(uri -> Glide.with(context).load(uri)
.into(usersViewHolder.image));
usersViewHolder.name.setText(user.getName());
usersViewHolder.city.setText(user.getCity());
}
// ONE USER STORAGE REFERENCE
public static StorageReference getUserPictureReference(String userMail, String gender) {
return getAllUsersStorageRef().child(gender).child(userMail).child("PROFILE PICTURE");
}
And here is the exception:
E/StorageException: StorageException has occurred.
Object does not exist at location.
Code: -13010 HttpResult: 404
E/StorageException: { "error": { "code": 404, "message": "Not Found. Could not get object", "status": "GET_OBJECT" }}
java.io.IOException: { "error": { "code": 404, "message": "Not Found. Could not get object", "status": "GET_OBJECT" }}
at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:434)
at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:451)
at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:442)
at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:286)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:70)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:62)
at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:76)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Please help me
I have found a solution for my problem. It was about path.
Earlier, when i was registrating the profil picture of the user in firebase storage, i also saved the path of the picture in firestore.
StorageReference userProfilePicture = FirestoreUsage.getUserPictureReference(Prevalent.currentUserOnline.getMail(), gender).child("profile_picture.jpg");
userProfilePicture.putFile(uriImageSelected).addOnSuccessListener(this, taskSnapshot -> {
String pathImageSavedInFirebaseStorage = Objects.requireNonNull(taskSnapshot.getMetadata()).getPath();
choiceMap.put("profile_picture", pathImageSavedInFirebaseStorage);
Prevalent.currentUserOnline.setProfile_picture(pathImageSavedInFirebaseStorage);
Then in my adapter, i created a Storage Reference with that path and dowloaded the pic
StorageReference storageReference = FirebaseStorage.getInstance().getReference(user.getProfile_picture());
storageReference.getDownloadUrl().addOnSuccessListener(uri -> Glide.with(context).load(uri)
.into(usersViewHolder.image));

Retrieving image from Firebase Storage

I know this is a discussed topic, but here I am, after all other solutions I've found did not solve my problem. I saved multiple images in Firebase Storage and now I want to get one of them and put it into an ImageView on an AlertDialog (or another Activity).
I've read about Glide solution, but it doesn't work for me. Gilde:
Glide.with(ViewScenesSG.this) //ViewScenesSG is the Activity
.load(reference) //the storage reference
.into(imageView); //my imageView
Dependencies for Glide (I've tried with multiple versions and 4.8.0 is the only one that doesn't give me Build error):
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
annotationProcessor 'androidx.annotation:annotation:1.1.0'
I've also read about the .using(new FirebaseImageLoader()) method, I've implemented the FirebaseImageLoader(), but the using() method is not found => build error. I believe it's been removed.
The second version I've tried is this one (found in another question here):
final long ONE_MEGABYTE = 1024 * 1024;
reference.getBytes(ONE_MEGABYTE)
.addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] bytes) {
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
imageView.setMinimumHeight(dm.heightPixels);
imageView.setMinimumWidth(dm.widthPixels);
imageView.setImageBitmap(bm);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(ViewScenesSG.this, "Couldn't retrieve image.", Toast.LENGTH_SHORT).show();
}
});
But every time it goes on the Failure listener. Therefore, my imageView always remains empty.
In my logcat I observed this error:
E/StorageException: Could not open resulting stream.
java.io.IOException: Could not open resulting stream.
at com.google.firebase.storage.StreamDownloadTask.createDownloadStream(com.google.firebase:firebase-storage##19.1.1:145)
at com.google.firebase.storage.StreamDownloadTask.access$000(com.google.firebase:firebase-storage##19.1.1:36)
at com.google.firebase.storage.StreamDownloadTask$1.call(com.google.firebase:firebase-storage##19.1.1:167)
at com.google.firebase.storage.StreamDownloadTask$1.call(com.google.firebase:firebase-storage##19.1.1:164)
at com.google.firebase.storage.StreamDownloadTask$StreamProgressWrapper.ensureStream(com.google.firebase:firebase-storage##19.1.1:325)
at com.google.firebase.storage.StreamDownloadTask$StreamProgressWrapper.access$100(com.google.firebase:firebase-storage##19.1.1:262)
at com.google.firebase.storage.StreamDownloadTask.run(com.google.firebase:firebase-storage##19.1.1:175)
at com.google.firebase.storage.StorageTask.lambda$getRunnable$7(com.google.firebase:firebase-storage##19.1.1:1072)
at com.google.firebase.storage.StorageTask$$Lambda$12.run(Unknown Source:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
E/StorageException: StorageException has occurred.
Object does not exist at location.
Code: -13010 HttpResult: 404
But the image is there, in the Storage, and the reference points directly to it:
storageRef = storage.getReference("photos").child(title); //for all images in the current folder named with the 'title' attribute
StorageReference reference = storageRef.child(imageName); //for the image I want to retrieve
What am I missing?
EDIT: Using the suggestion in the coments, I tried with:
reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Glide.with(ViewScenesSG.this)
.load(uri.toString())
.into(imageView);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(ViewScenesSG.this, "Retrieving image failed",
Toast.LENGTH_SHORT).show();
}
});
but this time I get the error:
E/StorageException: { "error": { "code": 404, "message": "Not Found. Could not get object", "status": "GET_OBJECT" }}
java.io.IOException: { "error": { "code": 404, "message": "Not Found. Could not get object", "status": "GET_OBJECT" }}
at com.google.firebase.storage.network.NetworkRequest.parseResponse(com.google.firebase:firebase-storage##19.1.1:433)
at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(com.google.firebase:firebase-storage##19.1.1:450)
at com.google.firebase.storage.network.NetworkRequest.processResponseStream(com.google.firebase:firebase-storage##19.1.1:441)
at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage##19.1.1:272)
at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage##19.1.1:286)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage##19.1.1:70)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage##19.1.1:62)
at com.google.firebase.storage.GetDownloadUrlTask.run(com.google.firebase:firebase-storage##19.1.1:76)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
I found the following solution (as suggested in the comments before):
Using the Firebase UI plugin with Gilde, I added the following dependency:
implementation 'com.firebaseui:firebase-ui-storage:6.2.0'
Now, having the StorageReference reference pointing to the image, and the ImageView in which the content must be uploaded, the following code retrieves the image and puts it in the ImageView:
Glide.with(ViewScenesSG.this) //this is the current Activity
.load(reference)
.into(imageView);
To be mentioned that the ImageView must have the dimensions set to a specific value, not using wrap_content or match_parent (at least for height in my case), otherwise it will appear as a small icon. This can also depend on how the image was saved before in the Storage.
Hope this will help someone else as well!
I'm not sure what's going wrong in your second snippet, but in the first snippet you seem to be passing a StorageReference to Glide. Since Glide doesn't know anything about Firebase Storage, that won't work.
Instead, you should get a download URL from Firebase Storage, which is a regular HTTPS URL that provides public access to the data, and pass that to Glide to render in the view.
Something like:
reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
Glide.with(ViewScenesSG.this)
.load(uri.toString())
.into(imageView);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});

Object Not Fount Exception when I am trying to set FirebaseStorage Image file in ImageView in Android

I am developing an app where I want to display uploaded image from firebase storage by setting it to an Imageview using url. I am using following code to get this done.
StorageReference storageReference = FirebaseStorage.getInstance().getReference("ImageFolder/"+imageId);
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.with(getContext()).load(uri).into(viewHolder.imageThumbnail);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
This throws an Exception as following
StorageException has occurred.
Object does not exist at location.
Code: -13010 HttpResult: 404
08-09 22:50:05.280 10915-11000/com.app.myapp E/StorageException: { "error": { "code": 404, "message": "Not Found. Could not get object" }}
java.io.IOException: { "error": { "code": 404, "message": "Not Found. Could not get object" }}
at bxr.a(:com.google.android.gms.DynamiteModulesC:424)
at bxr.a(:com.google.android.gms.DynamiteModulesC:1404)
at bxl.onTransact(:com.google.android.gms.DynamiteModulesC:53)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.internal.zzamj$zza$zza.zzss(Unknown Source)
at com.google.android.gms.internal.zzamm.zza(Unknown Source)
at com.google.android.gms.internal.zzamd.zza(Unknown Source)
Please help with the issue.
I have a same issues , in my app i was saving URL in Database and image in Storage.
just Clean entire STORAGE and DATABASE thing and its works well in my case.
or
i found it why its happen..
it returns non-null firebase.Promise containing string A Promise that resolves with the download URL or rejects if the fetch failed, including if the object did not exist.
Check here for this method getDownloadURL()
This occurs when there is not an item in firebase storage at the specified location.
Here is what the exception looks like in Android:
04-13 07:07:48.851 20923-22411/org.emeritus.globalivy E/StorageException: { "error": { "code": 404, "message": "Not Found. Could not get object" }}
java.io.IOException: { "error": { "code": 404, "message": "Not Found. Could not get object" }}
at bti.a(:com.google.android.gms.DynamiteModulesC:428)
at bti.a(:com.google.android.gms.DynamiteModulesC:1408)
at btc.onTransact(:com.google.android.gms.DynamiteModulesC:53)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.internal.zzbub$zza$zza.zzjM(Unknown Source)
at com.google.android.gms.internal.zzbue.zzd(Unknown Source)
at com.google.android.gms.internal.zzbtv.zza(Unknown Source)
at com.google.android.gms.internal.zzbtv.zze(Unknown Source)
at com.google.firebase.storage.zzb.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
It would be nice if the Firebase SDK didn't output the entire stacktrace to the logs and only this
StorageException has occurred.
Object does not exist at location.

Manage image with Firebase

I am developing an android app that using firebase. My app looks like
App shows card that contains image.
I don't know how to manage image when app works in offline mode.
Could you give me suggestion to resolve it ?
First, use offline capabilities of Firebase by using .setPersistenceEnabled(true);
Use a Image Managing Cloud Server like Cloudinary to store all your images.
In your Firebase database, save the Url to the image as a String.
Use a Image Library like Picasso to get your image from the url.
This worked for me, hope it helps you.
Use Picasso or similar library for load image, show it and save in memory.
Use Firebase only for upload image to cloud and get link.
Example upload image:
StorageReference storageReference = FirebaseStorage.getInstance().getReference(app.packageName + ".webp");
UploadTask uploadTask = storageReference.putFile(Uri.fromFile(new File(app.icon)));
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
success = false;
countDown.countDown();
Log.e(TAG, "", e);
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.i(TAG, "onSuccess");
app.setPublicIconPath(taskSnapshot.getDownloadUrl().toString());
ref.child(app.getFirebaseKey()).child("icon").setValue(app.getFirebaseValueDataPart1().getIcon());
}
});

Unable to load default image in image view while getting image url from server

I am using GET method to receive some JSON data from server.One of the jsonObject is given below:
{
"ID": "576",
"id": "c1b6d2d0d946504d09a471e05d3907c9",
"name": "Harminder Singh",
"last_logged": "1429535277",
"url": "common/marvel/photos/2015/04/fd64s_161_bd4c9ab730f5513206b999ec0d90d1fb",
"extension": "jpg",
"current_status": "icon-offline prople-offline",
"room_id": "a7aeed74714116f3b292a982238f83d2",
"current_user": false
}
I am getting image url here.Using api end point url and extension i am getting the absolute URL.
String absoluteURL = api + "/" + url + "." + extension;
I am using this absoluteURL to load the image in imageview using Picasso library.Now my problem is that at some urls images exist and at some not .SO i am getting some of the imageview as blank.How can i verify from the received URL that an image exists there or not?
You really can't. But you can use a placeholder, or even a loading image. You may find this blog post useful
There is no direct way to do that but what you can do is, override onimageLoadFailed() method. on load failure you can do whatever you want.
Picasso.Builder builder = new Picasso.Builder(this);
builder.listener(new Picasso.Listener(){
#Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception){
// your stuff
}
});
builder.build().load(URL).into(imageView);
Or if you have some default drawable to be put everytime there is an error in fetching than you can just add
.error(R.mipmap.some_drawable)

Categories

Resources