0

Application is working till showing image kite then its stopped by giving error OutOfMemory. How to resolve this problem in simple way.

I have 25 images not too large, decoding them through BitmapFactory showing in imageview, want to clear memory of imageview every time before showing the new image on button onClick().

how to do this? how i can use recycle option or array adapter in my app....please anyone help me out of this problem.

MyActivity.java

picasso coding

 Picasso.with(image.getContext())
                        .load(imageIds[currentIndex])
                        .transform(new BitmapTransform(MAX_WIDTH, MAX_HEIGHT))
                        .skipMemoryCache()
                        .resize(size, size)
                        .centerInside()
                        .into(image);

public class BitmapTransform implements Transformation {

    int maxWidth;
    int maxHeight;

    public BitmapTransform(int maxWidth, int maxHeight) {
        this.maxWidth = maxWidth;
        this.maxHeight = maxHeight;
    }

    @Override
    public Bitmap transform(Bitmap source) {
        int targetWidth, targetHeight;
        double aspectRatio;

        if (source.getWidth() > source.getHeight()) {
            targetWidth = maxWidth;
            aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            targetHeight = (int) (targetWidth * aspectRatio);
        } else {
            targetHeight = maxHeight;
            aspectRatio = (double) source.getWidth() / (double) source.getHeight();
            targetWidth = (int) (targetHeight * aspectRatio);
        }

        Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
        if (result != source) {
            source.recycle();
        }
        return result;
    }

    @Override
    public String key() {
        return maxWidth + "x" + maxHeight;
    }

};



@Override
public void onDestroy(){
    super.onDestroy();
    if (mp!=null)
        mp.release();
    finish();
}

}
Community
  • 1
  • 1
Newbie
  • 25
  • 6

4 Answers4

0

Often, working with even a few images and BitmapFactory you will run into OOME problems like this. You can try to do extensive memory management (like recycling bitmaps) and you should. You will run out of memory or have performance problems.

But even with proper memory management, you will get OOME problems without enabling additional heap memory size:

How to increase heap size of an android application?

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
0

When it comes to showing images dynamically I am using Picasso library. It handles in single line of code loading scaled images, caching, loading errors etc.

Picasso.with(context).load(R.drawable.landing_screen).fit().centerInside().into(imageView1);

You can also read Google Developers chapter about Displaying Bitmaps Efficiently if you really need to do it yourself. Definitely, you should move decoding bitmap to the background thread. You can use for that AsyncTask.

Damian Petla
  • 8,963
  • 5
  • 44
  • 47
  • I tried the Picasso library.But emulator is not responding and hanged again and again.Can Picasso work for ImageSwitcher and conditions can be applied??? how? @Loop – Newbie Aug 08 '14 at 03:44
  • From what you have shared with us it's hard to tell what's wrong. Would it be possible to share your entire project? I would reproduce it and check what is going on. – Damian Petla Aug 08 '14 at 07:39
  • As I said it is the simple app with just a button, on click images from drawable change in ImageView.When i simply run this app without decoding bitmap it was changing the images till fish.jpg then i applied the Bitmap Factory then it start to show images till kite.jpg the 11th pic in image array, then it stop by giving the error. I want to display all the images on click need to recycle the ScaledBitmap,"bitmap variable". but when I applied at the end of the onlclick event the emulator stop working and hang.this is my problem hope so now you understand. The entire code is mentioned above@Loop – Newbie Aug 08 '14 at 22:01
  • another thing i want to mention that when i applied condition in onlclick event with picasso that after showing last image array reset and start displaying images from beginning it crashes! without the condition picasso is working. – Newbie Aug 08 '14 at 22:03
  • Problem solved with picasso :) there was an error with the image file...but i also want to learn about recyle the bitmap. could you please guide me? @Loop – Newbie Aug 09 '14 at 14:34
  • I am glad it's solved. Please mark my answer as correct if it is working for you. Regarding recycle and bitmap memory management please read the following chapter from Android Developers page https://developer.android.com/training/displaying-bitmaps/manage-memory.html Use of recycle() method is mentioned. – Damian Petla Aug 09 '14 at 18:34
  • as u referred picasso, i'm using in my app but still having out of memory problem. i'hv total images size in my app is 15mb. how to handle it with picasso? does picasso has limits? please guide me.. – Newbie Aug 21 '14 at 11:15
  • Can you paste the line of code with Picasso? I would like to see how you do it. You can update your question. – Damian Petla Aug 21 '14 at 11:18
  • here it is the edited coding, i'm using MAX_WIDTH=700, MAX_HEIGHT=700. – Newbie Aug 23 '14 at 09:01
  • There is no need to provide `BitmapTranform` since `resize()` method do exactly the same. Moreover, 700px is a lot and it's not correct on every device. Why don't you try `fit()` method which is most optimized solution here. I have updated my answer including `centerInside()`. – Damian Petla Aug 23 '14 at 09:20
0
try 
{
    if (currentBitmap != null)
    {
        currentBitmap = null;
    }

System.gc();
} catch (RuntimeException e) {

} catch (Exception e) {

}

Bitmap currentBitmap = BitmapFactory.decodeResource(getResources(),R.id.someimage,options);

Jut Null bitmap before use it.

Niks
  • 500
  • 4
  • 7
-1

Does this work?

  • Create class variable Bitmap currentBitmap
  • At the end of the onClick() function set currentBitmap to equal scaledBitmap
  • At the beginning of the onClick() function add this code:

    if(currentBitmap != null)
     currentBitmap.recycle();
    

I have no time to test this right now, so let me know if it works!

The Heist
  • 1,444
  • 1
  • 16
  • 32
svanheste
  • 71
  • 1
  • 3