Cannot load Image from url using picasso - android

I use piccaso 2.5.2 and I have okhttp 2.4.0 and okhttp-urlconnection 2.4.0 in my gradle. The ImageView is a part custom list item layout. The error is that nothing appears in the ImageView. It occurs only for the first item of the list. That list is used to populate in SwipeFlingAdapterView which is similar to ListView. SwipeFlingAdapterView is from this library https://github.com/Diolor/Swipecards
Ok, here are my codes.
Picasso.with(mActivity)
.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile);
ImageView in xml file.
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/white"/>
The above code doesn't work so I try to log the error with this.
Picasso picasso = new Picasso.Builder(mActivity).listener(new Picasso.Listener() {
#Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
exception.printStackTrace();
Log.e("Picasso","description",exception);
}
}).build();
picasso.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile);
It doesn't show error. It doesn't even get into onImageLoadFailed. So, I try other way around.
picasso.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile, new Callback() {
#Override
public void onSuccess() {
if(vh.iv_profile.getHeight() == 0){
vh.iv_profile.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.img_default));
Toast.makeText(mActivity, "we get here", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onError() {
}
});
OnSuccess in callback, I can't even set Image from drawable anymore. If I set Drawable before using picasso.load, I can set it successfully. I also tried to use it without .centerCrop(). It doesn't work.
here is the final code.
try {
Picasso.with(mActivity).load(url).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("picasso error", "bitmap loaded");
if (bitmap != null) {
vh.iv_profile.setImageBitmap(bitmap);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Log.i("picasso error", width + ", " + height);
}
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}catch(Exception e){
Log.i("picasso error", "exception");
}
onBitmapLoaded, I get a bitmap with width and height of 243 but it doesn't appear in the ImageView.
Please help!

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 get loaded url from picasso library

I am using picasso library for set marker with image on google map api. But i need to get loaded drawable which loaded from picasso.
I tried this but its not returning the url which loaded from picasso
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
You cannot get the URL from Picasso after the drawable has loaded (Picasso.LoadedFrom will only tell you whether the image was loaded from DISK, MEMORY or NETWORK).
But since you need to specify the URL when you tell Picasso to .load() the image, you can simply store the URL somewhere and use it later on.
If your actual goal is to load a custom marker icon from a remote URL into a Google Maps marker, here is how to do it using an ad-hoc ImageView:
private void loadImageFromUrlIntoMarker(final String url, final Marker marker) {
final ImageView iv = new ImageView(context);
Picasso.with(context).load(url).into(iv, new Callback() {
#Override
public void onSuccess() {
Bitmap bm = ((BitmapDrawable) iv.getDrawable()).getBitmap();
try {
marker.setIcon(BitmapDescriptorFactory.fromBitmap(bm));
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Could not load image into marker.");
}
}
#Override
public void onError() {
Log.e(getClass().getSimpleName(), "Could not load image from " + url);
}
});
}
The try { ... } catch { ... } is important, as the marker might be invalid when the drawable becomes available.
basic usage of picasso
Picasso.with(context).load(my_url).into(my_view);
and import the library using gradle
compile 'com.squareup.picasso:picasso:2.5.2'
I dont know why you asking this question because you need an url to load image with Picasso . You can create a function basically like :
public void loadimage(final String loadingUrl){
Picasso.with(getApplicationContext()).load(loadingUrl).into(yourimageview, new Callback() {
#Override
public void onSuccess() {
Log.v("picasso","i successfully created image, here is url : "+loadingUrl);
}
#Override
public void onError() {
}
});
}

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)

Images are not loading in android 5.0 using picasso library

I am working on demo application in which I am using Picasso library v2.5.2. It is working fine on all android operating system version, but not in lollipop.
Image whose size is 130KB which is not loading for me. Images whose size is less are loading correctly.
Here is my code for downloading bitmap and set on imageview.
target = new Target() {
#Override
public void onPrepareLoad(Drawable drawable) {}
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
#Override
public void onBitmapFailed(Drawable drawable) {}
};
Picasso.with(this).load(URL).into(target);
I'm not sure what extra stuff I have to do with this so that I will work on lollipop also or this is bug in lib ?
It's a known problem. The problem is that Picasso keeps a weak reference for the Target. To get it working you need to make it strong, by storing a Target as a tag of view, for example.
target = new Target() {
#Override
public void onPrepareLoad(Drawable drawable) {}
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
#Override
public void onBitmapFailed(Drawable drawable) {}
};
imageView.setTag(target);
Picasso.with(this).load(URL).into((Target) imageView.getTag());
EDIT:
I suggest you to use Glide, it's very similar to Picasso, and also recommended by Google. And as you can see in the end of this thread, the original developer solves this BitmapFactory problem by using extra buffer.
Why would you use a Target if you only need to load the image into the ImageView? Just use this:
Picasso.with(this).load(URL).into(imageView, new Callback()
{
#Override
public void onSuccess()
{
//Dimiss progress dialog here
}
#Override
public void onError()
{
//And here
}
});
For documentation look here.
Picasso.with(this).load("http://webneel.com/wallpaper/sites/default/files/images/04-2013/island-beach-scenery-wallpaper.jpg").placeholder(R.mipmap.ic_launcher).fit().into(imageView, new Callback() {
#Override public void onSuccess()
{
}
#Override public void onError()
{
}
});
fit() will help you to load image.And use android:adjustViewBounds="true" in your ImageView in xml.

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