How to load image with Picasso to simple View? - android

I need to load image using Picasso/Glide into View, not ImageView.But there isn't such method. Are there any probabilities?
Thanks everyone for answers!

With Picasso, you can load into a Target, like this:
Picasso.with(context).load(xyz).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// Load the bitmap into your view
}
#Override
public void onBitmapFailed() {
}
});
Then you can do with the bitmap whatever you want, like loading it into a custom view.

If you want don't want Glide to insert the image directly into an ImageView, then you can always just load the image and do what you want with it, for example:
Glide.with(getContext())
.load(uri)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
// Do anything with the new Bitmap resource you have
}
});

Related

Load image from URI as layout background

I have used Picasso to load an image from my company's CDN into a ImageView:
ImageView imgView;
//...
Picasso.with(context).load(Uri.parse(url)).into(imgView);
But now I need load an image as a layout background:
RelativeLayout theLayout;
//...
Picasso.with(context).load(Uri.parse(url)).into(...);
Is it possible with Picasso? If not, should I use a ImageView instead of Relativelayout?
you can use glide to download bitmap and set it as background from any layout.
Glide
.with(getApplicationContext())
.load("https://www.google.es/images/srpr/logo11w.png")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
Drawable dr = new BitmapDrawable(resource);
theLayout.setBackgroundDrawable(dr);
// Possibly runOnUiThread()
}
});
but the better way is to use imageView on top of relativelayout and make it match_parent and show image on this imageview. this will help you directly use glide or picaso to load image in image view without memory errors.
Picasso.with(getActivity()).load(your url).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
yourlayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
edit:
you may need to override following methods as well
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.e("TAG", "Failed");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.e("TAG", "Prepare Load");
}
}
Yes. You can use Picasso for this. Please check following code :
Picasso.with(getActivity()).load(Uri.parse(url)).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
relativeLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
})

How to load bitmap in touchimageview through Glide SDK 4.3.1?

Am trying to load a file as bitmap into TouchImageview. If I use normal image view instead of Touch image view Glide library is able to load image into it from file object but in case on touch image view Glide unable to load image.
Used following code as well:
Glide.with(this).asBitmap().load(file).into(new SimpleTarget<Bitmap>(250, 250) {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
touchImageView.setImageBitmap(resource);
}
#Override
public void onLoadFailed(#Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
}
});
But OnLoadFailed() is called with errorDrawable as null.
Ok I just found about xml. please take note that, you HAVE TO USE RELATIVE LAYOUT!
<RelativeLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<my.zuhrain.kit.TouchImageView
android:layout_marginTop="20dp"
android:id="#+id/image_view_main"
android:layout_width="400dp"
android:layout_height="400dp" />
</RelativeLayout>
and here we go
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//things need to know
//USE RELATIVE LAYOUT!
Glide.with(getApplicationContext())
.asBitmap()
.load(uri)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
touchImageView.setImageBitmap(resource);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});

Drawable to bitmap efficiently using Picasso onBitmapFailed() method

I'm using target as callback mechanism (with Picasso).
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// loading of the bitmap was a success
// TODO do some action with the bitmap
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
// loading of the bitmap failed
// TODO do some action/warning/error message
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso
.with(context)
.load(...)
.into(target);
I want to load placeholder if bitmap fails to load, here:
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
My drawable placeholder is located locally in res/drawable folder.
Which is the best way to do the convertion from DRAWABLE to BITMAP?
FIRST WAY, (Alot of people suggests on SO this way):
Bitmap placeholderIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.placeholder);
SECOND WAY, (I think it should be more memory effiecient)
#Override
public void onBitmapFailed(Drawable errorDrawable) {
errorDrawable = getResources().getDrawable(R.drawable.poster_placeholder);
Bitmap placeholderIcon = ((BitmapDrawable) errorDrawable).getBitmap();
}

How to give unloaded imageview different colors until they are loaded, change Picasso code to glide

This easy to achieve using Picasso
Picasso.with(holder.mImageView.getContext())
.load(item.getUrl())
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
holder.mImageView.setImageBitmap(bitmap);
holder.mLoadingImageView.setVisibility(View.GONE);
holder.updatePalette();//the logic of generate diffrent background colors
Log.d(TAG, "on success");
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
holder.mLoadingImageView.setVisibility(View.GONE);
Log.d(TAG, "on error");
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
holder.mLoadingImageView.setVisibility(View.VISIBLE);
}
});
and the holder take care of that logic (getting different colors for unloaded image) from updatePalette function, here its code or the whole demo if you want
in glide what?
Glide.with(holder.mImageView.getContext())
.load(item.getUrl())
.into(/*WHAT*/);
Any duplication would help.
You can achieve the same using Target or SimpleTarget (implements Target) in glide.
i.e.
Glide.load("http://somefakeurl.com/fakeImage.jpeg")
.asBitmap()
.fitCenter()
.into(new SimpleTarget(250, 250) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
// Image Loaded successfully.
}
#Override
public void onLoadStarted(Drawable placeholder){
// Image Loading starts
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable){
// Image Loading failed
}
});
}
lastly I did it
private ColorDrawable[] vibrantLightColorList =
{
new ColorDrawable(Color.parseColor("#9ACCCD")), new ColorDrawable(Color.parseColor("#8FD8A0")),
new ColorDrawable(Color.parseColor("#CBD890")), new ColorDrawable(Color.parseColor("#DACC8F")),
new ColorDrawable(Color.parseColor("#D9A790")), new ColorDrawable(Color.parseColor("#D18FD9")),
new ColorDrawable(Color.parseColor("#FF6772")), new ColorDrawable(Color.parseColor("#DDFB5C"))
};
then
Glide.with(holder.mImageView.getContext())
.load(item.getUrl())
.placeholder(getRandomDrawbleColor())
.into(holder.mImageView);
and
public ColorDrawable getRandomDrawbleColor() {
int idx = new Random().nextInt(vibrantLightColorList.length);
return vibrantLightColorList[idx];
}
Keep PlaceHolder Until The Image is Loading Using Picasso's placeholder function
Picasso.with(context)
.load("image_url")
.placeholder(R.drawable.ic_launcher)
.into(imageView);
Methods of setting the placeholder Image are same in Picasso and Glid
In Glid
Glide.with(holder.mImageView.getContext())
.load(item.getUrl())
.placeholder(R.drawable.placeholder)
.into(imageView)
In Picasso
Picasso.with(holder.mImageView.getContext())
.load(item.getUrl())
.placeholder(R.drawable.placeholder)
.into(imageView)

android get Drawable image after picasso loaded

I am using Picasso library to load image from url. The code I used is below.
Picasso.with(getContext()).load(url).placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder).into(imageView);
What I wanna do is to get the image that loaded from url. I used
Drawable image = imageView.getDrawable();
However, this will always return placeholder image instead of the image load from url. Do you guys have any idea? How should I access the drawable image that it's just loaded from url.
Thanks in advance.
This is because the image is loading asynchronously. You need to get the drawable when it is finished loading into the view:
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
Drawable image = imageView.getDrawable();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
Picasso.with(this).load("url").into(target);
mImageView.post(new Runnable() {
#Override
public void run() {
mPicasso = Picasso.with(mImageView.getContext());
mPicasso.load(IMAGE_URL)
.resize(mImageView.getWidth(), mImageView.getHeight())
.centerCrop()
.into(mImageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Drawable drawable = mImageView.getDrawable();
// ...
}
#Override
public void onError() {
// ...
}
});
}
});

Categories

Resources