How to get image from Firebase storage? - android

I'm trying to get an image from my Firebase Storage in Android Studio
and set the image to ImageView. How can I get the image from Firebase storage?
What I've tried:
mStorageImage=FirebaseStorage.getInstance().getReference().child("Profile_Images").child("UserName");
mStorageImage.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.with(AccountView.this).load(uri).fit().centerCrop().into(profileImage);
}
});

You must select your image url form right side of the console inside the file location.The steps are given below
Step.1. Select Download location form your console(Its should be visible inside the file location)
Use Picasso or glide or whatever suits you to implement easily. Here I was implemented the glide library for display the images.
String url="Firebase download url";
You can also use activity Instant of myFragment
Glide
.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.crossFade()
.into(myImageView);

Related

cache image without showing glide

Is it possible to cache image using glide without showing it in the imageView?. If it is then how?.
Right now I'm doing this code:
Glide
.with(getApplicationContext())
.load("imageUrl")
.override(windowWidth(),(int)windowWidth()*0.5))
.diskCacheStrategy(DiskCacheStrategy.ALL);
But this is not working , when app is open glide load image not from cache but from url.
Since glide 4.X:
//to save img
RequestManager rm = Glide.with(context);
rm.load(imgUrl).submit();
//to load img
Glide.with(context)
.applyDefaultRequestOptions(
new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
.load(imgUrl)
.into(view);
Based on this GitHub topic
i've never used it, but referring the documentation: have you tried the downloadOnly?
Glide's downloadOnly() API allows you to download the bytes of an image into the disk cache so that it will be available to be retrieved later.
https://github.com/bumptech/glide/wiki/Loading-and-Caching-on-Background-Threads#downloadonly
To preload remote images and ensure that the image is only downloaded once:
Glide.with(context)
.load(yourUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.preload();

Android cannot load image from URI using Picasso

The Android app allows users to select a photo from their phone gallery which I save the URI to realm. I then retrieve this information and use Picasso to load it into the image view. For some reason the image is not loaded.
The URI is something like:
content://com.android.providers.media.documents/document/image%3A333180
I save it to realm using mCategory.icon = imageURI.toString() and then when I load it:
Picasso.with(getContext())
.load(Uri.parse(mCategory.icon)) // mCategory.icon is a string
.resize(200, 200)
.error(R.drawable.mountain) // default image to load
.into(viewHolder.categoryIcon);
The URI you got is a ContentProvider URI to show it with Picasso you try to do this:
File file = new File(Uri.parse(mCategory.icon));
Picasso.with(getContext())
.load(file)
.resize(200, 200)
.error(R.drawable.mountain)
.into(viewHolder.categoryIcon);
P.S: Though you can convert your ContentProveder path to Absolute path still it's better if you don't since Picasso support loading from the File..
Use setLoggingEnabled(true) in picasso to check the log if there is any error message.
Mentioned URI contains the encoded value of colon : i.e. %3A.
Either rename the source OR try Glide or some other library to serve the purpose.
This is working perfectly with new version of Picasso 2.71828
Picasso.get()
.load(imageUri)
.placeholder(R.drawable.placeholder)
.fit()
.centerCrop()
.into(viewHolder.categoryIcon);

show google drive image with glide in android imageview

My drive url is -
https://drive.google.com/file/d/0B3DFHb2-MdGYa2NMUkVtVkZ1V1k/view?usp=sharing
I want to show it in android imageview using glide. I have tried many ways to show it.
Following is my code to show -
Glide.with(getActivity()).load(readExcelFeed.getImage().toString()).into(ivQueImage);
Please let me know what is the issue in it.
Steps for loads image from Google drive into ImageView by Glide Library
Step 1 : In your Drive which image you want to display on ImageView. You need to get shareable link from the three dots from the google drive. like this "https://drive.google.com/open?id=139jBj_GUfmFi_pZN38SS9RMB5wNXMEy9"
Step 2: Then the base url for display the image. link is
public static String BASE_URL = "https://drive.google.com/uc?id=";
Step 3 : In the Step 1 you see the id of the image then add this id in the base URL. Like this type
"https://drive.google.com/uc?id=139jBj_GUfmFi_pZN38SS9RMB5wNXMEy9"
Step 4 : Use Glide library for display the image on ImageView Like this:
Glide.with(mContext)
.load("https://drive.google.com/uc?id=139jBj_GUfmFi_pZN38SS9RMB5wNXMEy9")
.into(holder.user_image);
Now you can see your image on your ImageView.
Unless I'm missing something, you want to show just that single image using Glide. Just right click on it, select "Copy image address", then use that as your url String for Glide.
String url = "https://lh3.googleusercontent.com/s6-0yxPhDS4Os7SYbP66RCNp-mDwuKr72mLJx9ekuTWYFPgXahCvj-oVatwXELyVfyZzD80YTaiYde4=w1278-h954-rw";
Glide.with(getActivity()).load(url).into(ivQueImage);
For request image source you must use executeMediaAndDownloadTo()
service.files().get(fileId).executeMediaAndDownloadTo(outputStream);
Then you can get byte[] from output stream
byte[] data = outputStream.toByteArray();
and insert it to Glid
RequestOptions options = new RequestOptions();
options.skipMemoryCache(true);
options.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide
.with(this)
.load(data)
.apply(options)
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);

Cache images local, from google firebase storage

I am searching for a way, to cache images from the storage on google firebase platform. For now, I can download images, and show these to users, but I am not able to cache this, and access, even without internet connection. The database can be accessed offline. So I thought, there would be a way for storage too. I don't want to download every single image to storage, cause then I would need to check everytime, if the image is still up to date, it may be changed. Here are few links, what I could find, but no answer for my question. Maybe someone know a workaround, or a way how to accomplish it. Thanks!
Download files:
https://firebase.google.com/docs/storage/android/download-files
Cache (offline) database:
https://firebase.google.com/docs/database/android/offline-capabilities
UPDATE 1
Here is how I "cache" files with picasso, I added activity, that cares the download:
Picasso.with(getApplicationContext())
.load(uri.toString())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(image1);
Any help is welcome. Thanks!
I'm afraid the Firebase SDK doesn't provide image caching by itself. But there are several great libraries that could do it for you. They download image, show it in an ImageView and cache it in a single line of code. Just request the Firebase for an image download url and feed it to the image caching library.
Something like this, if you choose Picasso as a caching library:
storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
// Pass it to Picasso to download, show in ImageView and caching
Picasso.with(context).load(uri.toString()).into(imageView);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
UPD: To use disk caching with Picasso you need to explicitly setup OkHttpDownloader. Look here How do I use disk caching in Picasso?
Thanks to #ATom for notice. Api has changed, and FirebaseUI 3.0 now uses Glide 4.x
Here is updated sample:
To load an image from a StorageReference, first register in your
AppGlideModule:
#GlideModule
public class MyAppGlideModule extends AppGlideModule {
#Override
public void registerComponents(Context context, Glide glide, Registry registry) {
// Register FirebaseImageLoader to handle StorageReference
registry.append(StorageReference.class, InputStream.class,
new FirebaseImageLoader.Factory());
}
}
Then you can load a StorageReference into an ImageView:
// Reference to an image file in Cloud Storage
StorageReference storageReference = ...;
// ImageView in your Activity
ImageView imageView = ...;
// Download directly from StorageReference using Glide
// (See MyAppGlideModule for Loader registration)
GlideApp.with(this /* context */)
.load(storageReference)
.into(imageView);
And don't forget to add dependency in your build.gradle:
implementation 'com.firebaseui:firebase-ui-:3.1.0'
Answer source on GitHub
Old answer:
FirebaseUI 1.0 now released. Storage example has class FirebaseImageLoader
Images displayed using FirebaseImageLoader are cached by their path in
Firebase Storage, so repeated loads will be fast and conserve
bandwidth.
// 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);
I had the same issue. Tried all possible ways but couldn't fix that. I think the problem lies with the links generated by firebase storage. Picasso generally caches the images it loads. I have tried other cloud services like cloudinary, LibPixel whose links end with image format (Eg:.jpg) and picasso caches these link images. While firebase links end with token numbers. Strange thing it fails!

Load image from URL to imageView as well as Cache

Hi i am new beginner in android. I want to insert image in imageView from URL but whenever one time it is loaded from URL in imageView then second time it should be insert without internet means it would be stored in cache as well.
for this you can use picasso Library
You can download it from Picasso Library site simply put this jar file into libs folder. It will manage all property of Image.
http://square.github.io/picasso/
String imgURL = "Your URL";
ivmage = (ImageView) findViewById(R.id.imageView1);
Picasso.with(MainActivity.this).load(imgURL).into(ivmage);
You can use Picasso Library.
By Using Picasso, The Advantages are
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.
To load Image from Url
Picasso.with(context).load(url).into(imageView);
Refer this link for Api : Picasso
you can use Glide https://github.com/bumptech/glide
Glide.with(this).load("imageURl").into(imageView);
I suggest picasso library for doing this. here is the detailed documentation of picasso library.
ex:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
or you can make use of volley's ImageLoader. here you can find the documentation for Volley's image loading.
ex :
// Retrieves an image specified by the URL, displays it in the UI.
ImageRequest request = new ImageRequest(url,
new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap bitmap) {
mImageView.setImageBitmap(bitmap);
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
mImageView.setImageResource(R.drawable.image_load_error);
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(request);
if you are using volley. then you have to cache images manually. picasso will cache images by default.

Categories

Resources