Firebase storage URL keeps changing with new token - android

I'm trying to build a social media application using firebase database and storage. Below is the flow expected.
User upload a profile picture which is stored on firebase storage in the current user folder and the URL stored in firebase database for quick access. (Works fine)
User post their thoughts. This save users info such as post message, username and profile image URL in databases. (Works fine).
Problem
The problem now is say a user updates he's or her profile picture, this overrides the older profile image in firebase storage (in order manage storage and to make user image be the same across all comments and post). On the post message activity the older profile image URL can't be accessed cause the token as changed.
Question
I will like to know how this can be fixed in such that the firebase storage URL will be static (that is the same) accross all updates.
NB
Using Picasso and not firebase method to get the images

Although this question had been asked for a very long time, but I noticed some people still find it difficult solving this, so I will be providing my solution below.
STEP 1
Since storage URL are always dynamic (i.e the posses token) when changed, what I did was to use generic name for image file stored say avatar for all users
STEP 2
Create a directory in storage for each user as follows: users/{uid}/avatar.jpg
STEP 3
Pull or display the image by using the path in step 2 (see below)
ANDROID
StorageReference storageReference = FirebaseStorage.getInstance().getReference("users").child(userId).child("avatar.jpg");
Glide.with(context).using(new FirebaseImageLoader()).load(storageReference).diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.drawable.ch_white_icon).placeholder(R.drawable.ch_white_icon).into(imageView);
WEB
var storage = firebase.storage();
var pathReference = storage.ref('users/' + userId + '/avatar.jpg');
pathReference.getDownloadURL().then(function (url) {
$("#large-avatar").attr('src', url);
}).catch(function (error) {
// Handle any errors
});
With this you don't have to worry with the dynamic link anymore, whenever you upload a new image to the above path, it overrides the previous one and the code in step 3 will give you the new image.
PS: For Android Don't forget to change DiskCacheStrategy.ALL to DiskCacheStrategy.NONE if you don't want glide to cache image or you can use timestamp to get new image if cache is allowed.

Since the URL image is stored in the database, you could use a Cloud Function to update the value after a user has updated his picture.
You can trigger a Cloud function in response to the updating of files in Cloud Storage, see:
https://firebase.google.com/docs/functions/gcp-storage-events
You will find examples of Cloud Functions at: https://github.com/firebase/functions-samples
and the full doc at: https://firebase.google.com/docs/functions/

Related

Uploading image to Firebase Storage gives exception [duplicate]

When users log in with Facebook (the only way to log in currently), and want to post something, I run some code to upload their profile picture first, so other users can see who the original poster is. It seems like I can get the user's profile picture Uri which is for example (taken from debug mode):
https://graph.facebook.com/(FACEBOOKID)/picture?height=200&width=200&migration_overrides=%7Boctober_2012%3Atrue%7D
However, when I run:
StorageReference posterPicture = mStorage.child("UsersPictures").child(profile.facebookID);
posterPicture.putFile(profile.profilePictureURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {...});
It throws an error saying "no content provider"
W/System.err: java.io.FileNotFoundException: No content provider: https://graph.facebook.com/(FACEBOOKID)/picture?height=200&width=200&migration_overrides=%7Boctober_2012%3Atrue%7D
Error Code: -13000,
HTTP Response Code: 0
Thanks
The short answer is that the URI has to be a file on your file system (you have to download it and re-upload it), or you need a content resolver that can handle HTTPS scheme'ed files.
That said, my question to you is: why? The file is already hosted on Facebook and easily retrievable by that URL, so why upload it again? You can very easily store and share that URL in the Firebase Realtime Database, no need to pay to host in in Firebase Storage.

Error when trying to upload facebook Uri to Firebase Storage

When users log in with Facebook (the only way to log in currently), and want to post something, I run some code to upload their profile picture first, so other users can see who the original poster is. It seems like I can get the user's profile picture Uri which is for example (taken from debug mode):
https://graph.facebook.com/(FACEBOOKID)/picture?height=200&width=200&migration_overrides=%7Boctober_2012%3Atrue%7D
However, when I run:
StorageReference posterPicture = mStorage.child("UsersPictures").child(profile.facebookID);
posterPicture.putFile(profile.profilePictureURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {...});
It throws an error saying "no content provider"
W/System.err: java.io.FileNotFoundException: No content provider: https://graph.facebook.com/(FACEBOOKID)/picture?height=200&width=200&migration_overrides=%7Boctober_2012%3Atrue%7D
Error Code: -13000,
HTTP Response Code: 0
Thanks
The short answer is that the URI has to be a file on your file system (you have to download it and re-upload it), or you need a content resolver that can handle HTTPS scheme'ed files.
That said, my question to you is: why? The file is already hosted on Facebook and easily retrievable by that URL, so why upload it again? You can very easily store and share that URL in the Firebase Realtime Database, no need to pay to host in in Firebase Storage.

addOnfailurelistener being called when I try uploading an image to firebase storage

pic2I am new to firebase storage and I am trying to upload an image to firebase just so I could learn it. My app has a button. When I click on it. Image gets displayed on image view. The image gets displayed correctly, but that image is not stored to firebase storage. I have added the necessary code (OnClickListener of buttonpic1) from firebase website to upload that image to storage. But the image isnt uploaded and addOnfailurelistener gets called (I am using a toast in this method). Can anybody let me know why it is not being uploaded on firebase?
Firebase needs Google Play Services version 9.4.0 or newer in order to work, as explained here, so try to update it and check again.
Also, check if you have specified the appropriate rules for your Firebase Project. By default, Firebase Storage allows only authenticated users, but you can change it to public.
Go to the "Rules" tab of the "Storage" section in the console, and replace the code you see by that:
service firebase.storage {
match /b/your-bucket-id.appspot.com/o {
match /{allPaths=**} {
allow read, write;
}
}
}
where your-bucket-id put the address shown in the "Files" tab in the console (without gs://).

Setting profile image from google cloud storage bucket in android

I am using google cloud storage for storing profile images for one of the android app which we are developing. I am able to store images to the cloud properly..I want to set the same image to imageview in my application using picasso API.
It is saying Access denied, When I share Image on cloud publicly, it is setting image to Imageview, otherwise not...
How could I give these public share through android application for user to set images using Picasso?
Thank you in advance
When you upload the image you must make sure (assuming you don't want authentication) the ACL is set to publicRead.
The upload in java looks like this:
storage.objects()
.insert(BUCKET_NAME, null, content)
.setName(fileName)
.setPredefinedAcl("publicRead")
.setKey(API_KEY)
.execute();
For setting the ACL manually in the developers console tick the "shared publicly" check box.
The url then looks like this:
http://storage.googleapis.com//
Good luck.

Get image thumbnail from google drive in Android

Is it possible to get image thumbnail from google drive image file ? When I'm opening the native google drive app, it shows me thumbnails of images, but when I'm opening the file chooser of API, it's shows me default icon for all images.
Google Drive Android API doesn't provide thumbnails at the moment, they will be likely to be available on the new version.
It's very simple, actually.
On the file (com.google.api.services.drive.model.File) you get back, just call getThumbnailLink(). That will be the thumbnail of the image. However, if it's not an image, if I remember correctly, it will simply be blank (not null, but just blank).
Update: I had an error in the discussion of the request fields.
As far as I can tell, there is literally no good documentation on the solution for this. It's true that you can't get thumbnails with the Drive Android API, however you can get thumbnails with the Drive Java Client Library. This page has is a really good primer for getting started:
https://developers.google.com/drive/v3/web/quickstart/android
Oddly, I can't get the fields portion of the request to work as it is on that quick start. As I've experienced, you have to request the fields a little differently.
Since you're doing a custom field request you have to be sure to add the other fields you want as well. Here is how I've gotten it to work:
Drive.Files.List request = mService.files()
.list()
.setFields("files/thumbnailLink, files/name, files/mimeType, files/id")
.setQ("Your file param and/or mime query");
FileList files = request.execute();
files.getFiles(); //Each File in the collection will have a valid thumbnailLink
A sample query might be:
"mimeType = 'image/jpeg' or mimeType = 'video/mp4'"
Hope this helps!

Categories

Resources