ImageView content loading from to another ImageView - android

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);
}
});

Related

Why doesn't Picasso display full images?

I have an android app that uses Picasso to load images. Problem is that it doesn't load the image the first time. It just gives me an empty fragment. It loads it fine after I return back to my thumbnail activity and tap the same image again.
Picasso
Picasso.with(
context)
.load('url')
.into(mImage);
It is also worth mentioning that it has no problem loading the full image (frame) of a video file that is part of my album collection. That one I tap on it and it loads the image no problem. I don't know why.
Now, I did some research and looked around a few forums where people seemed to be having my same problem and they recommend Glide. Promptly I set it up in my project and give it a try and my images work, I get full size images the first time I select a thumbnail.
Glide:
Glide.with(context)
.asBitmap()
.load('url')
.into(new BitmapImageViewTarget(mImage)
{
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition)
{
super.onResourceReady(resource, transition);
mImage.setImageBitmap(resource);
mImage.setZoom(1);
}
});
My question is: why did Glide provided me with a better result than picasso did? What was I missing to make Picasso work?
well , i guess nothing wrong with Picasso , i think it's just some delay caused by internet connection .
Glide and Picasso are technically the same , and you should use the last version of them next time
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
and also , since we are talking about images , you have to know that both Picasso and Glide have issues with loading some https url and for that i advice you to use Universal Image Loader

Efficient way to load large number of same image in ReyclerView

I developed a chat app. It uses RecyclerView to show chat info. It's exactly like facebook messenger.
My problem is, loading the image of user I'm chatting to takes time, and make the app slow when there's large number of chat messages.
I'm using picasso to load image.
Picasso.get()
.load("https://domain/images/profile_picture/" + otherImage)
.networkPolicy(NetworkPolicy.OFFLINE)
.transform(new CircleTransform())
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
int placeHo;
if(otherGender.equals("female"))
placeHo = R.drawable.ic_female_color;
else
placeHo = R.drawable.ic_male_color;
Picasso.get()
.load("https://domain/images/profile_picture/" + otherImage)
.error(placeHo)
.transform(new CircleTransform())
.centerCrop(Gravity.TOP)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
Log.v("Picasso","Could not fetch image");
}
});
}
});
Is there any efficient way to show the image? as it's showing the same image(user profile picture).
I can think only one reason that your RecyclerView is lagging. Because your image size is large.
See, if your chat image is about 50-100dp then you should use the same resolution image. Perhaps you are loading original image.
AFAIK I use Glide over Picasso because Glide optimize the downloaded image, as ImageView size.
See Glide doc
Glide's primary focus is on making scrolling any kind of a list of
images as smooth and fast as possible, but Glide is also effective for
almost any case where you need to fetch, resize, and display a remote
image.
You don't need to worry about cache in Picasso and Glide both. Cache is enabled by default in Picasso, so if same image is queried again, then it will be picked from cache.
Solution 1 (using Piccaso)
Resize image as much you need.
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.resize(100, 100) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
Solution 2 (Using Glide)
You need not to worry about anything, you are good to go if you use Glide.
keep it Static
Or You can set it at the time of initialisation
not changing it every time on viewholder
sorry for my english and format of writing
If you are using the same image in all the Views of the same viewType then load the image in onCreateViewHolder method of the adapter and not in onBindViewHolder.
The same View with the image already loaded will be recycled (reused) and not loaded again and again, the result will be for sure faster.
You have to postpone the first image retrieve procedure at when the RecyclerView.OnChildAttachStateChangeListener() event signals that a child is added. Obliviously the Event is triggered for each of firsts visible items. The most correct way to postpone something like this is to create an Handler and then send/enqueue a Handler.sendMessage(Message.obtain(..)) one for each visible objects) items. Inside the Handler you will receive that Message and there you can execute the Picasso action.
Then you have to decide if load other (the not yet visible items) images all at once or just prepare the picture only when it is required by the Recycler.

Display multiple image with Glide

I want to display one image into two different image views using glide.I know that i can simply use the code below to do it.
ImageView imageView = findViewById(R.id.header_image);
Glide.with(this).load(R.drawable.header_image).into(imageView);
ImageView imageView2 = findViewById(R.id.header_image);
Glide.with(this).load(R.drawable.header_image).into(imageView2);
But it requires to load the same image twice into memory and i don't want that due to memory issues.I want to load the image once and diaplay it into two image views.What can i do to achieve it?
You no need to worry about memory issue while using glide because glide has own caching system to optimize memory pls read this doc
Glide provides a number of options that allow you to choose how loads will interact with Glide’s caches on a per request basis.
Disk Cache Strategies
DiskCacheStrategy can be applied with the diskCacheStrategy method to an individual request. The available strategies allow you to prevent your load from using or writing to the disk cache or choose to cache only the unmodified original data backing your load, only the transformed thumbnail produced by your load, or both.
The default strategy, AUTOMATIC, tries to use the optimal strategy for local and remote images. AUTOMATIC will store only the unmodified data backing your load when you’re loading remote data (like from URLs) because downloading remote data is expensive compared to resizing data already on disk. For local data AUTOMATIC will store the transformed thumbnail only because retrieving the original data is cheap if you need to generate a second thumbnail size or type.
To apply a DiskCacheStrategy:
GlideApp.with(fragment)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
Loading only from cache
In some circumstances you may want a load to fail if an image is not already in cache. To do so, you can use the onlyRetrieveFromCache method on a per request basis:
GlideApp.with(fragment)
.load(url)
.onlyRetrieveFromCache(true)
.into(imageView);
If the image is found in the memory cache or in the disk cache, it will be loaded. Otherwise, if this option is set to true, the load will fail.
You can use following code to load an image once and display it in multiple imageviews.
Glide.with(this)
.asBitmap()
.load(R.drawable.header_image)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
imageview.setImageBitmap(resource);
imageview2.setImageBitmap(resource);
}
});
If you are using latest version of Glide then create RequestOptions like
private RequestOptions simpleOptions = new RequestOptions()
.centerCrop()
.placeholder(R.color.color_gray)
.error(R.color.color_gray)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
Use RequestOptions to load image with set properties
Glide.with(context)
.load(url)
.apply(simpleOptions)
.into(imageView);
Or maybe this way…
RequestBuilder<Drawable> builder = Glide.with(this).load(drawableResId);
builder.into(imageView0)
builder.into(imageView1)
try this way ...
setImage(R.drawable.header_image,imageView ,imageView2 );
void setImage(int image, ImageView... imageView) {
for (ImageView imageView : imageView) {
if (imageView != null) {
Glide.with(this).load(image).into(imageView);
}
}
}

Loading image in ImageView using volley library

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
} };

How to first show thumb image from URL in NetworkImageView and request a large size image in background and show when large one downloaded?

I am using Volley and I have a single NetworkImageView with two image urls, one small thumb image and other large full size image. I want to first check in cache for large image, if it found then show in NetworkImageView else check thumb image in cache if found then show in view and if not found then first load thumb image from a url in NetworkImageView.
at the same time if large image not found in cache then request large image url in background and replace thumb image with large image when download completed.
problem is NetworkImageView taking drawable resource id only as a default placeholder or error image, it should take dynamic url too
i have solved my issue using below code:
Picasso.with(getActivity())
.load(thumbImageUrl)
.error(defaultImageUrl)
.placeholder(defaultImageUrl)
.into(ImageView_sliderImage, new Callback()
{
#Override
public void onSuccess()
{
if(imageUrl!=null && imageUrl.length()>0)
{
Picasso.with(getActivity())
.load(biggerImageUrl)
.error(defaultImageUrl)
.placeholder(ImageView_sliderImage.getDrawable())
.into(ImageView_sliderImage);
}
}
#Override
public void onError()
{
}
});

Categories

Resources