Hi, I want, to use Picasso for compound drawables since It's doing a great job for ImageViews. But it doesn't really work. The place where the image should come just stays white.
This is what I've done so for. bitmap is the URL of the Image.
Picasso.with(getActivity().getApplicationContext()).load(bitmap).into(new Target() {
public void onBitmapLoaded(Bitmap bitmapPic, LoadedFrom from){
Drawable d = new BitmapDrawable(getResources(), bitmapPic);
textView[picassoCounter].setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
textView[picassoCounter].setCompoundDrawablePadding(5);
}
public void onBitmapFailed(Drawable errorDrawable){
Drawable d = getResources().getDrawable(R.drawable.nofavicon);
textView.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
textView.setCompoundDrawablePadding(5);
}
public void onPrepareLoad(Drawable placeHolderDrawable){
}
});
You are not holding a strong reference to your Target. It gets gc'ed before Picasso gets to load it.
That was a really fast answer. Thank you! But I'm a beginner with android development. This is the first app I'm using. I know of WeakReferences, but the only thing I know of them is that I used them once. I have no idea what a Strong Reference is. Could you tell me where I need to do what? I'm Sorry, I know that you don't want to give me the full solution and want me to do something on my own but I'm stuck since many hours on this problem.
private class MyActivity extends Activity {
private Target target = new Target { ... }
// Then later in your code
private void loadImages() {
picasso.load(url).into(target);
}
}
Your activity now holds a reference to target preventing it from being gc'ed until the activity gets destroyed.
Also ensure the URL works and the bitmap can be downloaded. I recommend you rename the variable to bitmapUrl or something to make it more clear.
Hi, thanks for the reply again. I'm not using an Activity. I'm using a Fragment that extends an own ScrollViewListener. Also the image Loading has to be done inside a for-loop that is inside an AsyncTask. Maybe a bit more tricky. So what I went with, was creating the private Varaible target at the start of the asynchronous class. Then, in the doInBackground method I assigned the methods for the target. Then, I loaded it this way
Picasso.with ( getActivity().getApplicationContext() ).load ( baseUrl).into ( target );
but that gives me just a NullPointerException at this line
source[picassoCounter].setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
The good part is, that it shows, that this is called, right? Is it a problem, if the target = new Target( ) {blablabla} is inside the for-loop? I mean it has to, right? Since I'm using the forloop to iterate through my textview array to add compound drawables.
If you are inside an async task you should not use load() instead you should use get(). You are already in a background thread.
Hmm thanks... Will have to look more into detail for this. Seems like I'm not ready yet to use Picasso. Will just google a bit more around to understand everything better. Thanks for your support though!
Subclass TextView and implement Target on that class. Have the onBitmapSuccess callback set the compound drawable. Use the subclass in your XML or code instead of TextView.
@dnkoutso the solution that you are suggesting didn't work inside fragment in the first time when the fragment is created (but when I left the fragment and I open it again it work) this is my code :
private Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
myView.getRootView().setBackgroundDrawable(drawable);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
the call inside onCreatView
Picasso.with(getActivity()).load(R.drawable.image_fond_ma_carte).into(target);
@selmanon, look at this solution http://stackoverflow.com/questions/24180805/onbitmaploaded-of-target-object-not-called-on-first-load#answers
It seems because of WeakRefence, in the link above is the answer or "Subclass TextView and implement Target on that class", like said JakeWharton above.
TextView subclass, as suggested by Jake Wharton. In Kotlin, with Anko bindings.
There's a solution for this here. PicassoTargetableTextView
Most helpful comment
Subclass
TextViewand implementTargeton that class. Have theonBitmapSuccesscallback set the compound drawable. Use the subclass in your XML or code instead ofTextView.