get image with picasso based on userId firebase - android

I am trying to do the image upload and download for a user profile, when I save a photo I save it with the userId so I can replace it everytime he changes his photo, now what I need is to get that photo with picasso, the thing is, I don't know how to get the photo, it can have different formats and other stuffs like jpeg, png, I just want to get the image when the userID match, I tried this:
storageRef = storage.getReferenceFromUrl("gs://friendlymanager-3b7a2.appspot.com");
Picasso.with(UserSettings.this).load(storageRef + "/Photos/" + userId).into(userImage);
my userImage is my imageView, i don't get any erros just a blank image, i already have a photo for the specific user.
Any help?

you will have to get download link like this,
StorageReference storageRef = storage.getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child(FirebaseAuth.getInstance().getCurrentUser().getUid());//reach out to your photo file hierarchically as stored on firebase
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Log.e("URI", uri.toString());
Glide.with(SaveUserActivity.this).load(uri).into(profile_image);
url = uri.toString();
}
});

Consider using Firebase-UI Android library which gives you ability to load images from storage ref directly. In your case it would look something like this
I'm not sure if Picasso is supported but you can use Glide
for example:
mStorageRef = FirebaseStorage.getInstance().getReference();
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(mStorageRef + "/Photos/" + userId)
.error(R.drawable.default)
.into(imageView);

You should use getDownloadUrl() while fetching images from Firebase storage using Picasso OR glide etc.

Related

How to get download url of Thumbnail after firebase resize images extension completets

The recently introduced firebase extension 'Image resize' produces a thumbnail once a picture is uploaded to a storage bucket.
How do I obtain the download url of the image of this thumbnail, after the extension completes?
final StorageReference storageRef =
FirebaseStorage.instance.ref().child(fileName);
final StorageUploadTask uploadTask = storageRef.putFile(
File(path),
);
final StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
final String url = (await downloadUrl.ref.getDownloadURL()); //This will give me the download url of file before resize
// ??How do I the download url of resized image that gets stored in fileName/thumbnails folder
When you upload an image file to your specified Cloud Storage bucket, this extension:
Creates a resized image with your specified dimensions.
Stores the resized image in the same Storage bucket as the original uploaded image.
Names the resized image using the same name as the original uploaded image, but suffixed with your specified width and height.
For example, if you specify a path here of thumbs and you upload an image to /images/original.jpg, then the resized image is stored at /images/thumbs/original_200x200.jpg
So your file's url will be-
String name = url.substring(url.lastIndexOf("/")+1,url.indexOf("."));
String urlStr = "thumbnails/"+name+"_"+width+"x"+height+url.substring(url.indexOf("."),url.length());
storageRef.child(url.replace(name,urlStr)).getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
}})

How to correctly download firebase storage images into recycelrView

I have recyclerview and in every row I have an image that I load with firebase storage. It seems like the image loading is affecting the scrolling performance of the recyclerView.
I am using glide to load the images that I got from firebase inside onBindViewHolder by calling imageLoadGlide method like this:
//Download image from firebase Storage and set gameImage("ImageView") image.
private void imageLoadGlide(DocumentSnapshot documentSnapshot, final QuestionsHolder questionsHolder) {
//for firebase storage
FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReference();
storageRef.child(documentSnapshot
.get("image").toString())
.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//this part is loading image from url library
Glide
.with(context.getApplicationContext()) // pass Context
.load(uri)// pass the image url
.centerCrop() // optional scaletype
.crossFade() //optional - to enable image crossfading
.transform(new CircleTransform(context))//transfoms the imageView onto circle with the custon class CircleTransform
.into(questionsHolder.gameImage); // the ImageView to which the image is to be loaded
//stop the loading bar from spinning
questionsHolder.loadProgress.setVisibility(View.GONE);
}
});
}
The download works fine but it makes the scrolling super slow.
I have no idea why this is happening because I compress the images before uploading them so I don't think that this is a problem of image weight.
The image compress is made like this :
Bitmap bitmap = ((BitmapDrawable) questionImage.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = mountainImagesRef.putBytes(data);
Any ideas on why this is happening and how can I avoid this?
Here is onBindViewHolder for #Vadim Eksler request:
#Override
public void onBindViewHolder(#NonNull final QuestionsHolder holder, final int position) {
holder.gameImage.setImageBitmap(null);
setInfoForViews(holder, result.get(position));
imageLoadGlide(result.get(position), holder);
setOnClicksListners(holder, position);
setRankTextView(position, holder);
if (position == result.size() - 1 && !endOfDocs && next != null) {
loadMoreDocs(position);
}
}
This answer is based on all the comments together.
So as mentioned in the comments what I did wrong is that every time that onBindViewHolder was called I have pulled the image from firebase again and again - this causes poorly recyclerView performance.
What I did to fix it:
I have used flyweight design pattern to load the image only for the first time from firebase and after that simply recycle this image for the next time onBindViewHolder will get called.
First I created a map as a global variable:
private Map<String, Bitmap> flyweight = new HashMap<>();
after that when the image is loaded from the first time I will save it for later when onBindViewHolder will get called again:
Inside onBindViewHolder :
if (flyweight.get(result.get(position).get("image").toString()) == null) {
imageLoadGlide(result.get(position), holder); // load the image
} else {
//recycle the image that i already have
holder.gameImage.setImageBitmap(flyweight.get(result.get(position).get("image").toString()));
}
And the last thing is to add my image to the map that I created after the image is successfully pulled :
flyweight.put(documentSnapshot.get("image").toString(), resource);

Show .gif from Firebase Storage link

I have link in my Firebase Storage on .gif file
String gifUrl = "https://firebasestorage.googleapis.com/v0/b/rokitskiydev-a18ca.appspot.com/o/WF%2F10-16-02-Digital-15.gif?alt=media&token=0354f6e6-4292-4911-9302-49281d293a80";
I try use Picasso and Glide. But I can show only image of this gif.
GlideApp.with(context).load(gifUrl).into(ImageView);
If i get another link, an example http://i.imgur.com/1ALnB2s.gif it's OK!
How can I do this?
Get the downloadUrl of the gif and then use that to load the gif with Glide.
ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(downloadUrl).into(imageViewTarget);
For newer versions of Glide try this:
Glide.with(context)
.load(imageUrl)
.asGif()
.placeholder(R.drawable.loading2)
.crossFade()
.into(imageView);
As Glide Documentation says,
You can use .asGif() to force the library to load an animated gif and fail if it is not :
Glide.with(activity).load(gifUrl).asGif().into(view);
try this
Check the doc: https://futurestud.io/tutorials/glide-displaying-gifs-and-videos
also as FirebaseDoc says you can do this to load an image/gif with glide
first declare this in your build.gradle
dependencies {
// FirebaseUI Storage only
compile 'com.firebaseui:firebase-ui-storage:0.6.0'
}
then
this to load your image with firebase
// Reference to an image file in Firebase Storage
StorageReference storageReference = ...;
// ImageView in your Activity
ImageView imageView = ...;
// Load the image using Glide
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageReference)
.into(imageView);
if you need the url of your image/gif file, you can do this
storageRef.child("your/firebase/imagepath.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
generatedFilePath = downloadUri.toString(); /// The string(file link) that you need
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});

Image from StorageReference Firebase-Android

TextView myUsername=(TextView) findViewById(R.id.user);
myProfile=(ImageView) findViewById(R.id.photo);
Bundle bundleObject=getIntent().getExtras();
myAccount=(Account) bundleObject.getSerializable("account");
myUsername.setText(myAccount.get_username());
StorageReference profile =
FirebaseStorage.getInstance()
.getReference().child("profiles").child(myAccount.getUID()).child("profile");
profile.getDownloadUrl().addOnSuccessListener(Profile.this, new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
myProfile.setImageURI(uri);
}
});
I'm trying to put the image from the Firebase Storage in the ImageView but is not showing and I don't have any error . What I'm doing wrong?
ImageView doesn't load images from the internet itself - and the URL retrieved from Storage is a regular HTTPS one. The setImageURI method is for loading from the local file system.
You can use a library like Glide or Picasso to do the image loading - if you take a look at FirebaseUI, you'll find a helpful integration that includes caching: https://github.com/firebase/FirebaseUI-Android/tree/master/storage

Firebase Google Sign In - Photo URI

I was able to get the photo URI using this code from the firebase doc
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
for (UserInfo profile : user.getProviderData()) {
// Id of the provider (ex: google.com)
String providerId = profile.getProviderId();
// UID specific to the provider
String uid = profile.getUid();
// Name, email address, and profile photo Url
String name = profile.getDisplayName();
String email = profile.getEmail();
Uri photoUrl = profile.getPhotoUrl();
};
}
i also have an ImageView called mPic and I tried this code.
mPic.setImageURI(null);
mPic.setImageURI(photoUrl);
unfortunately, I cannot view the photo. Any tips on how to do this?
Try to use picasso library. http://square.github.io/picasso/
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.resize(imgWidth, imgHeight)
.centerCrop()
.into(image);
Picasso.with(getApplicationContext())
.load(mAuth.getCurrentUser().getPhotoUrl())
.resize(50, 50)
.centerCrop()
.into(yourImageView);
I was just trying to retrieve the image from "photoUri" to be used in Jetpack Compose.
After countless attempts I found out that the URI actually holds the photo's url, we just need to extract it.
In my app, I extracted the url with this code (Kotlin):
fun Uri?.getUrl(): String? = this?.run { "https://$host$path" }
After getting the URL, you are free to use it in any way.
In my use case, I used it with coil.
AsyncImage(
model =
ImageRequest.Builder(LocalContext.current)
.data(url) //Url retrieved from the above function
.crossfade(true)
.build(),
contentDescription = "User image",
)
Note: You can also directly use the "photoUri" in data [.data(photoUri)] as well

Categories

Resources