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.
Related
I am creating an app that uploads a photo to the real-time database by saving in under a token as a getDownloadUrl ()
Exemple for a url: https://firebasestorage.googleapis.com/v0/b/reshetgoonmedicalbook-d9a44.appspot.com/o/Briza%2F2022%2F%D7%A8%D7%A4%D7%95%D7%90%D7%99%2F%D7%A0%D7%95%D7%91%D7%9E%D7%91%D7%A8%2F%D7%91%D7%93%D7%99%D7%A7%D7%94%20%D7%91%D7%93%D7%99%D7%A7%D7%94-000000000%2F1669726258516.jpg?alt=media&token=3030d9f7-b501-48ec-a0e0-e00d77ee95f5
And it shows the img on the app from the same url.
The problem is that for the first couple of times everything works fine,i can download the photo and see it but randomly for random urls it stops working.
I am creating an app that uploads a photo to the real-time database by saving in under a token as a getDownloadUrl ()
Exemple for a url: https://firebasestorage.googleapis.com/v0/b/reshetgoonmedicalbook-d9a44.appspot.com/o/Briza%2F2022%2F%D7%A8%D7%A4%D7%95%D7%90%D7%99%2F%D7%A0%D7%95%D7%91%D7%9E%D7%91%D7%A8%2F%D7%91%D7%93%D7%99%D7%A7%D7%94%20%D7%91%D7%93%D7%99%D7%A7%D7%94-000000000%2F1669726258516.jpg?alt=media&token=3030d9f7-b501-48ec-a0e0-e00d77ee95f5
And it shows the img on the app from the same url.
The problem is that for the first couple of times everything works fine,i can download the photo and see it but randomly for random urls it stops working.
Exemple for how the error looks like in the app(blank square): the numbers of the blank squares are still the same as the numbers of the links
And when i try the url from the realtime database. Some urls works and other give me the ERROR 403 no permission
The rules are; { "Rules":{ "read":true, "write":true } }
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/
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.
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://).
I'm making something like online journal app, that will download "journal file" from Google Drive (via shared link) once and will update that file if it changes. Please, can anyone point me to some guides how to do it. I already tried to pin file from drive, but I don't really understand what to do next..
Downloading Files from Google Drive:
To download a Google Drive file via link, try this (from this tutorial):
https://drive.google.com/uc?export=download&id=FILE_ID
Just replace the FILE_ID with the original fileID found in the Drive URL.
Additonal notes:
You can download files using the DRIVE REST API
To download files, you make an authorized HTTP GET request to the file's resource URL and include the query parameter alt=media. For example:
GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs
Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content. For example, an app using the drive.readonly.metadata scope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting the viewersCanCopyContent field to true.
Updating files in Google Drive
Make an HTTP Request to Google Drive using PATCH. The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request- URI. Things to take note of are:
Parameters
Authorization
Request Body