Loading image in ImageView using volley library - android

I stored the path to my images in mysql db and was not sure if there's a way to load them in an imageView using volley library.
I'm able to parse string as json and display it in textView using volley string request queue but wasn't sure how I can get the path to display as image in my imageView. I would like to fetch the image using id.
Thanks in advance!

I always use the Glide to load images like this. Simply return the path like you do any other string. There is no need to load a bitmap with Volley, using Glide for this is simpler and better. Glide will handle for example image caching amongst other things.
This is all you need except from the Volley part.
ImageView imageView = findViewById(R.id.image);
String url = "www.foobar.com/" + path;
Glide.with(context).load(url).into(imageView);

Recently, I used Picasso library in my project and it worked fine and smooth. You can easily Load Image to ImageView from Url by using Picasso. Moreover, you can resize the original image to your desire size.
Example:
String mURL = "https:\/\/c.tribune.com.pk\/2017\/12\/1593389-vanga-1514310781-708-160x120.jpg";
ImageView Icon = (ImageView) findViewById(R.id.icon);
Picasso.with(context).load(mURL).resize(72, 72).into(icon);
Note: Using Picasso first you must import its library.
I used this: compile 'com.squareup.picasso:picasso:2.5.2'

use glide to display your image or you can use picasso

new ImageRequest(
url,
listener,
maxWidth,
maxHeight,
decodeConfig,
errorListener);
Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap bitmap) {
//here you set the bitmap to imageview
} };

Related

To display image in ImageView where drawable image ids stored in database column using Picasso

I am using Picasso to set image into ImageView from database where I have stored the drawable image id in the database. It is working perfectly when I store URL(eg https://loremflickr.com/g/320/240/paris)but it's not working for (eg R.drawable.team).
[database image]
//image is the database column containing image ids
Context context = imageText.getContext();
Picasso.with(context)
.load(image)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.resize(50, 50)
.into(imageview);
You to pass the path of the image to set image.Use Glide library to set image it is easy and have more functionality than picasso
Picasso is only a library that makes rendering the image easier through the uri or url path, without picasso you have to create an asyncronous script that is quite complicated. But if you have dynamic data for your drawing path, you can do it with regular looping.
They are now supporting loading Image from URI like the following :
Picasso.get().load(R.drawable.landing_screen).into(imageView1);
Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.get().load(new File(...)).into(imageView3);
From picasso v2+ here is a big modification. The new version is very helpful for manage cash data. It's using Singleton Instance.
By the way, please don't save the drawable ID, just store the drawable name so that later when you call it use the method like the following
private void loadImage(String mImageName, ImageView mImageIcon){
int resID = mContext.getResources().getIdentifier(mImageName , "drawable", mContext.getPackageName());
if(resID!=0) {//The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
mImageIcon.setImageResource(resID);
}
}

How can I download image using Glide lib with volley response

I'm facing problem with glide image loading, currently I'm using the given code which I searched from stackoverflow, but not working with me,
Details:
is volley response I have string url and below code I'm using but working in my condition, need help
#Override
public void webServiceCompleted(JSONObject responseJson) {
Gson gson = new Gson();
message_model=gson.fromJson(responseJson.toString(),myModelClass.class);
String url= message_model.getMessage_image();
commonFuntions.mLoaderCancel(mContext);
Glide
.with(getApplicationContext())
.load(url)
.into(_ImageView);
}
But after loading the screen remains blank, even I can't using placeholder for it.
The way you are trying to load your image url is correct, Problem is with the url of the image, append "http://" in the beginning of the response url and then try your original code again. It should work.
This is for load imageurl into imageview using glide.
library for glide : compile 'com.github.bumptech.glide:glide:3.7.0'
Glide.with(getApplicationContext()).load(Stringurl)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(yourimageview);
but first check your response.

Loading images in recyclerview with picasso from api

Add image in RecyclerView from api using picasso
Image loading using Picasso is very easy, you can do it like this way Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView)
In the new versions of Picasso you don't need the context anymore.
I'm using:
implementation 'com.squareup.picasso:picasso:2.71828'
In the new versions of Picasso used get()
Picasso latest version Picasso library: 2.71828
Picasso
.get()
.load("You Img Path Place Here")
.resize(width, height)
.noFade()
.into("Path set ImageView");
Used latest picasso version
implementation 'com.squareup.picasso:picasso:2.71828'
Try this
String img_url = "YOUR IMAGE URL";
if (!img_url.equalsIgnoreCase(""))
Picasso.with(context).load(img_url).placeholder(R.drawable.user_image)// Place holder image from drawable folder
.error(R.drawable.user_image).resize(110, 110).centerCrop()
.into("IMAGE VIEW ID WHERE YOU WANT TO SET IMAGE");
Happy coding.
You can load image using picasso like this way
Picasso.with(context).load(android_versions.get(i).getAndroid_image_url()).resize(120, 60).into(viewHolder.img_android);
https://www.learn2crack.com/2016/02/image-loading-recyclerview-picasso.html
Here ctx is context, list.getImage_full_path()) is image coming from server, in placeholder a static image is placed if image is not loaded, on error kent is name of image and into(servicecart_image) is placed in XML so image is loaded there
Picasso.with(ctx).load(list.getImage_full_path()).resize(160, 200).placeholder(R.drawable.kent)
.error(R.drawable.kent).into(servicecart_image);

ImageView content loading from to another ImageView

If it possible to write ImageView to another ImageView?
For example:
ImageView image1= (ImageView)findByVie(R.id.image1)
ImageView image2= (ImageView)findByVie(R.id.image2)
Glide.with(context).load(url).into(image1);
image2=image1
// I want to write image from image 1
//to image 2 without loading it again from internet.
Use this:
ImageView image1 = (ImageView)findViewById(R.id.image1);
ImageView image2 = (ImageView)findViewById(R.id.image2);
Glide.with(context).load(url).into(image1);
image2.setImageDrawable(image1.getDrawable();
However, with the way Glide works, it caches images it has loaded so calling Glide.with(context).load(url).into(image2); again would just load the image from the cache, not from the remote server again.
EDIT:
If you call getDrawable straight after calling Glide then it won't work, because Glide is still fetching the image so the first imageview is still empty. You need to use glides callback in order to make sure the image has been loaded before calling getDrawable
Glide.with(context).load(url).listener(new RequestListener).into(image1);
Bitmap bm=((BitmapDrawable)imageView1.getDrawable()).getBitmap();
imgview2.setImageBitmap(bm);
As your are using Glide to load image from url. Glide will use its cache to load image for second imageview it won't use internet to load same image in second imageview.
You can also use the following way to load image once for sure
Glide.with(this).load("url").into(new SimpleTarget<GlideDrawable>() {
#Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
image1.setImageDrawable(resource);
image2.setImageDrawable(resource);
}
});

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