2

I have looked around but have not found a solid solution to my problem. My application runs however when I naviagate around activities using bitmaps, I get the error, "Throwing OutofMemoryError "Failed to allocate 8294412 byte allocation with 690632 free bytes". I have snippets of my code below, used in two seperate activities. I have tried recycling the bitmaps, but that has not worked out well either.

 public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.dialogprofile, null);
            view.setTag(holder);
        } else {

            holder = (ViewHolder) view.getTag();
        }

        Uri myUri = Uri.parse(worldpopulationlist.get(position).getFilename1().toString());
        File f = new File(worldpopulationlist.get(position).getFilename1().toString());
        Bitmap myImg = BitmapFactory.decodeFile(myUri.getPath());
        Matrix matrix = new Matrix();
        Bitmap circleBitmap = Bitmap.createBitmap(myImg.getWidth(), myImg.getHeight(), Bitmap.Config.ARGB_8888);
        BitmapShader shader = new BitmapShader (myImg,  TileMode.CLAMP, TileMode.CLAMP);

        Paint paint = new Paint();
        paint.setShader(shader);
        Canvas c = new Canvas(circleBitmap);
        c.drawCircle(myImg.getWidth()/2, myImg.getHeight()/2, myImg.getWidth()/2, paint);
        int finalHeight, finalWidth;
        holder.success.setImageBitmap(circleBitmap);
        circleBitmap.recycle();
        int width=800;
        int height=700;
        finalHeight = holder.event.getMeasuredHeight();
        finalWidth = holder.event.getMeasuredWidth();
        filenamee = worldpopulationlist.get(position).getFilename().toString();
        Uri myUr = Uri.parse(worldpopulationlist.get(position).getFilename().toString());
        File f1 = new File(worldpopulationlist.get(position).getFilename().toString());
        Bitmap myImg1 = BitmapFactory.decodeFile(myUr.getPath());
        myImg1=Bitmap.createScaledBitmap(myImg1, width, height, true);
        Matrix matrix1 = new Matrix();
       holder.event.setImageBitmap(myImg1);
}
});
return View;
}

public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.list_row, null);
            view.setTag(holder);
        } else {

            holder = (ViewHolder) view.getTag();
        }

        Uri myUri = Uri.parse(worldpopulationlist.get(position).getFilename().toString());
        File f = new File(worldpopulationlist.get(position).getFilename().toString());
        Bitmap myImg = BitmapFactory.decodeFile(myUri.getPath());
        Matrix matrix = new Matrix();
        Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
                matrix, true);
        holder.flag.setImageBitmap(rotated);
}
});
return View;
}
Akhil Yeleswar
  • 87
  • 1
  • 1
  • 12
  • 1
    Look at this question: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object. My favourite answer there is the one that says "NEVER use `Bitmap.createBitmap(width, height, Config.ARGB_8888)`. I mean NEVER!". I have to concur. No matter how hard I try I can't stop that method crashing my app. – Paul Boddington Dec 25 '14 at 20:40
  • @pbabcdefp what specifically in my code should I add/replace? – Akhil Yeleswar Dec 25 '14 at 21:10
  • @pbabcdefp I use it a lot. The only thing you are missing is understanding how bitmaps allocate memory on the heap. Width x height * 4 bytes. Simple. – Simon Dec 25 '14 at 21:31
  • @AkhilYeleswarapu The error is clear. You are trying to allocate about 8mb when your heap has only 6.5mb. What size are the bitmaps? – Simon Dec 25 '14 at 21:33
  • @Simon You're completely right - I don't understand. How should I, and the OP, use this width * height * 4 bytes knowledge to avoid an `OutOfMemoryError` at runtime? I don't have control over the available memory do I? – Paul Boddington Dec 25 '14 at 21:36
  • @pbabcdefp Of course you do. You have complete control over what objects you create, what size bitmaps you use and what format you load bitmaps in. If you want to know how much heap an unscaled bitmap, or one using ARGB888 takes, that's the formula. MAT will also show you exactly how many objects you create and where. If developers have no control, how do you think apps manage to load high quality images without crashing? – Simon Dec 25 '14 at 21:38
  • @Simon I don't know. That's what I'm asking. I can easily simulate an app that "works" by using Bitmap.createBitmap in a try block and using the catch block to clear my cache of bitmaps (although it still occasionally throws the exception very soon anyway) but it's not very satisfactory. But that answer I linked to, and the number of upvotes it has, certainly indicates that this is not easy. – Paul Boddington Dec 25 '14 at 21:47

0 Answers0