Picasso - how to fetch image sequentially - android

I wanna retrieve images in Picasso for later and keep them in cache, at the moment I use this code:
for(int i=0; i<urlList.size(); i++) {
Picasso.with(getActivity())
.load(url.get(i))
.fetch();
}
but I wanna retrieve them sequentially. What's the best for that?

You can load the image into a Target, if you have an array of your urls you can recursively load them from the Target.
Example activity that does this:
public class MainActivity extends ActionBarActivity{
private ArrayList<String> urls;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urls = new ArrayList<>();
urls.add("http://placehold.it/300&text=0");
urls.add("http://placehold.it/300&text=1");
urls.add("http://placehold.it/300&text=2");
urls.add("http://placehold.it/300&text=3");
urls.add("http://placehold.it/300&text=4");
loadImages(0);
}
private void loadImages(final int index){
Target t = new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from){
Log.w("image", "image received" + index);
loadImages(index + 1);
}
#Override
public void onBitmapFailed(Drawable errorDrawable){
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable){
}
};
if(index < urls.size())
Picasso.with(this)
.load(urls.get(index))
.into(t);
}
}

Related

Issues with loading bitmaps with Picasso library in onPostExecute()

I've got a problem loading bitmaps with Picasso in a loop. I'm loading them in AsyncTasks onPostExecute(). I know that i need global Target variables to prevent them from being collected by GC. Still none of my bitmaps are loaded. Here's my code:
private static Map<Integer, Bitmap> markerBitmaps;
private String[] markerUrls;
private Integer[] catIds;
//some code
private final static List<Target> targets = new ArrayList<Target>();
#Override
protected void onPostExecute(Void result) {
final int[] id = new int[1];
int counter = 0;
for(String s : markerUrls) {
id[0] = catIds[counter];
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
targets.remove(this);
markerBitmaps.put(id[0], bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
targets.remove(this);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Log.i("BITMAP URL IS:", s);
targets.add(target);
Picasso.with(MapsMenuActivity.this)
.load(s)// Start loading the current target
.into(target);
counter++;
}
Log.i("BITMAPS NUM: ", String.valueOf(markerBitmaps.size()));
}
The size of my markerBitmaps map is always 0.

How to clear images loaded by Picasso on Android?

I have a BackgroundImageActivity that load background image using Picasso. But when I go back to home (onStop() called in BackgroundImageActivity) and go into another instance of this activity, which is supposed to load another background image, but at first 2 seconds, it still shows the image from previous BackgroundImageActivity. Is this some kind of cache?
How I can clear this image so that whenever I go into a new instance of BackgroundImageActivity, i don't see the image from previous one?
public class BackgroundImageActivity extends Activity {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initBackground();
}
#Override
protected void onStop() {
super.onStop();
Picasso.invalidate(imageUrl, getApplicationContext());
}
private void initBackground() {
...
}
private void setBg(final String imageUrl, final int bg) {
this.imageUrl = imageUrl;
final RequestCreator picassoRequest = Picasso.load(imageUrl, bg).memoryPolicy(MemoryPolicy.NO_CACHE);
targetReference = new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
...
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
...
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
...
}
};
picassoRequest.placeholder(bg).error(bg).into(targetReference);
}
}
Thanks!!!
Use this piece of code while loading the image.
Picasso.with(getContext())
.load(data.get(pos)
.getFeed_thumb_image())
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(image);
Picasso set the image to ImageView directly without caching in the background. This makes the app lightweight also.

How to check that all the files uploaded by Picasso lib?

I want before render MainActivity check what all pictures is loaded:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
getBitmapsByURLs(urls);
}
public void getBitmapsByURLs(List<String> urls) {
final List<Target> targets = new ArrayList<>();
for (int i = 0; i < urls.size(); i++) {
final int k = i;
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("TEMP", "Loaded: " + k);
targets.remove(this);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
targets.remove(this);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.i("TEMP", "Preparing: " + k);
}
};
targets.add(target);
Picasso.with(this)
.load(urls.get(i))
.memoryPolicy(NO_CACHE, NO_STORE)
.into(target);
}
}
How I can check inside onCreate what all pictures is loaded?
If I write while on method onCreate (after call getBitmapsByURLs) then it will be called before all pictures are loaded.
You must be known that when you use
Picasso.with(this)
.load(urls.get(i))
.memoryPolicy(NO_CACHE, NO_STORE)
.into(target);
to load a picture,it is a asynchronized task.First picasso download the picture from internet.After downloading is finished,the picture will be loaded into target.Of course,when downloading,the main thread will never be waiting.Picasso use something like Observer-Subcriber or interface Future to make the main thread informed that downing is over.Above all,no matter what you write after getBitmapsByURLs(urls),it will be called before picasso finishes loading pictures.(Because it usually costs some more time to download pictures and load them into targets).
What's more.This is not a recommended way to do such a time-costing work in onCreate().Because onCreate() is called in main thread.If you do something costs much time here,then you will get an Application Not Responding error.

Picasso only loading a few images

I am trying to pass a URL to be converted to a bitmap to be used in the activity. I have a class of "Projects" and everything loads fine from the API, including the String Url for the image.
My code looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discover);
ButterKnife.bind(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
adapter.add(new CardModel(mProject.mName, mProject.mTagline, bitmap));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.d("ISSUE:: ", "IMAGE NOT LOADED");
}
};
adapter = new SimpleCardStackAdapter(this);
mController = new Controller(this);
mController.startFetchingProjects();
}
#Override
public void onFetchProgress(Project project) {
mProjectList.add(project);
mImageUrls.add(project.mImageUrl);
}
#Override
public void onFetchComplete() {
for (int i = 0; i < mProjectList.size(); i++){
mProject = mProjectList.get(i);
mImage = mImageUrls.get(i);
Picasso.with(this).load(mImage).into(target);
}
}
}
For some reason even though there are 24 projects, when I try to load an image, it returns only about 8 random images (the rest go through onPrepareLoad for some reason). If I adapt my projects with no images, I'll have 24 projects, but when I include this picasso method, I only get about 8 images returned, and thus only 8 projects returned. Any idea how to fix this?

Using Picasso library with ImageSwitcher is causing Object out of Memory errors

I want to load large bitmaps efficiently in my Image Switcher and i have been using Picasso for this purpose but now i am stuck at this point. Without Picasso a lot of OOMs and other nasty exceptions please tell me if I can use this library with Image Switcher too.
If yes then provide sample code.
Thanks!
imswitch.setFactory(new ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
return imageView;
}
} );
And onClick:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if(currentIndex==messageCount)
currentIndex=0;
ImageView imageView = (ImageView)findViewById(R.id.imswitch);
Picasso.with(getApplicationContext()).load(imageIds[currentIndex]).into(imageView);
Toast.makeText(getApplicationContext(), "Pressed "+currentIndex,Toast.LENGTH_LONG).show();
}
One of the possible way is to create own implementation using Target interface.
https://square.github.io/picasso/javadoc/com/squareup/picasso/Target.html
public class ImageSwitcherPicasso implements Target {
private ImageSwitcher mImageSwitcher;
private Context mContext;
public ImageSwitcherPicasso(Context context, ImageSwitcher imageSwitcher){
mImageSwitcher = imageSwitcher;
mContext = context;
}
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
mImageSwitcher.setImageDrawable(new BitmapDrawable(mContext.getResources(), bitmap));
}
#Override
public void onBitmapFailed(Drawable drawable) {
}
#Override
public void onPrepareLoad(Drawable drawable) {
}
}
Than just use as follow
ImageSwitcherPicasso mImageSwitcherPicasso = new ImageSwitcherPicasso(getActivity(), playerBackground);
Picasso.with(getActivity()).load(new File(path)).into(mImageSwitcherPicasso);
where playerBackground is reference to ImageSwitcher

Categories

Resources