8
votes

I'm using Picasso library for load images in a viewpager, but I need to load a bitmap and Picasso accept only File variable. How can I load bitmap file using Picasso?

Bitmap imagescompress = decodeSampledBitmapFromResource(getResources(), AnohanaAdapter.imagep[position], 100, 100);

If I put imagecompress in the load function of Picasso, I've this error: Cannot resolve method 'load(android.graphics.Bitmap)'.

Actually for load images I use below code:

Picasso.with(VistaSingola.this).load(AnohanaAdapter.imagep[i]).placeholder(R.drawable.ic_launcher).error(R.drawable.error).fit().into(imageViewTouch);

But images are big and I don't want to go in OutOfMemory. Someone can help me? Please.

Thank you all help me

1
The image is too large to process, requiring too much memory. Try to use resize(x, y) and see what happens. - shkschneider
First of all thank you for your answer, i tried resize(x, y), but as i written in this post stackoverflow.com/questions/23371597/…, that code being in a "for loop" takes a long time to the app, I wanted to know if there is a way to convert the Bitmap variable in a variable that the library Picasso accepts. - Matteo
Converting Bitmap into File would take a lot (lot) longer. I'm not aware of a way to speed what you want to do (sorry). Only solution seems to me to lower the work you're doing, because image processing is always gonna take time if you have many. How much BTW? - shkschneider
Actually I've 50 images 1920 x 1080 and the app work without crash, but i want a dynamic resizing because, I see that when i go in viewpager the app continue to create Grow-Heap Frag Case, and I don't know if this is good or not. - Matteo

1 Answers

1
votes

You can use bellow code to do this

private Target target = new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        //TODO: Store bitmap in global vaiable
      }
      @Override
      public void onBitmapFailed() {
      }
}

private void loadBitmap(String url) {
   Picasso.with(this).load(url).into(target);
}

@Override 
public void onDestroy() {  // could be in onPause or onStop
   Picasso.with(this).cancelRequest(target);
   super.onDestroy();
}