I use Picasso library. And ran into problems and errors:

E / Bitmap ﹕ createBitmap error!

Code example:

Picasso .with(this) .load(object.getImages([1]) .error(R.drawable.video_cap) .into(exVideo); 

At first, everything works, but later (when I look at the second circle) the crash of "Out of memory" comes out. I used the fit() and resize() methods - the application runs longer, but ends the same way.

Having processed the error:

 try { Picasso.with(this) .load(object.getImages([1]) .error(R.drawable.video_cap) .into(exVideo); } catch (OutOfMemoryError e) {} 

application no longer crashes. But the photos are no longer loaded and the application starts to slow down. As I understand it, Picasso took all the RAM and there is no more space for new photos.

I need to somehow erase photos from RAM to display new ones.

Using memoryPolicy(MemoryPolicy.NO_CACHE) will not work. I cache the photos on the SD card by writing this code in the Application class:

 Picasso.Builder builder = new Picasso.Builder(this); builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE)); Picasso built = builder.build(); Picasso.setSingletonInstance(built); 

I keep links to pictures in the database, and Picasso then successfully loads them.

So I have 3 questions:

  1. how to free up space in RAM for new pictures
  2. how to cache only the right pictures on the SD card, not all
  3. how to handle the error "E / Bitmap ﹕ createBitmap error!"

By clicking on the button, I upload new images

 if (object.getImages().length > 1) { try { Log.e("test", "load picasso 1"); Picasso.with(this).load(object.getImages()[1]).fit().tag("tag1").error(R.drawable.video_cap).into(exVideo); }catch (OutOfMemoryError e) { Log.e("test", "error 1" + e); Picasso.with(this).load(object.getImages()[1]).fit().memoryPolicy(MemoryPolicy.NO_CACHE).into(exVideo); }catch (Exception e){ Log.e("test", "error 11" + e); } } 
  • Attach the code exactly where you are uploading the images - Android Android

1 answer 1

Try using instead of .into(exVideo); use the following code that scales the image before displaying it:

 .into(new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { int targetWidth = exVideo.getWidth(); float ratio = (float) bitmap.getHeight() / (float) bitmap.getWidth(); float heightFloat = ((float) targetWidth) * ratio; final android.view.ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) holder.image.getLayoutParams(); layoutParams.height = (int) heightFloat; layoutParams.width = (int) targetWidth; holder.image.setLayoutParams(layoutParams); holder.image.setImageBitmap(bitmap); } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } });