Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9071503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:55:59+00:00 2026-06-16T17:55:59+00:00

having trouble with handling a java.lang.OutOfMemoryError: bitmap size exceeds VM budget error. The original

  • 0

having trouble with handling a java.lang.OutOfMemoryError: bitmap size exceeds VM budget error. The original pictures are never bigger then 250x250px. and loaded from the drawable folder. I found some solutions across the internet talking about ‘inJustDecodeBounds’ but I just can’t get it to work.. Any ideas on how to fix this issue? It’s causing me a headache for two days now…

Right now I am rescaling the image by a factor which I calculate based on the parent width..

@Override
    public View getView(int position, View v, ViewGroup parent) {
        View mView = v;
        this.parent = parent;

        if (mView == null) {

            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(R.layout.caa_xml, null);
        }

        ImageView image = (ImageView) mView.findViewById(R.id.iv_caarow);

        String name = getItem(position).getFile();
        int resId = C.getResources().getIdentifier(name, "drawable",
                "com.test.com");
        int imageWidth = (int) calculateImageWidth();
        // load the origial BitMap (250 x 250 px)
        Bitmap bitmapOrg = BitmapFactory
                .decodeResource(C.getResources(), resId);

        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = imageWidth;
        int newHeight = imageWidth;

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize 
        matrix.postScale(scaleWidth, scaleHeight);
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);

        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        image.setImageDrawable(bmd);

        if (mView != null) {

            //additional code here

        }
        return mView;
    }

    private float calculateImageWidth() {
        // TODO Auto-generated method stub
        int parentW = parent.getWidth() - parent.getPaddingLeft()
                - parent.getPaddingRight();
        Resources r = C.getResources();
        float pxPaddingBetweenItem = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 2, r.getDisplayMetrics());
        float pxPaddingInItem = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics());

        int totalImageWidth = (parentW - (int) (3 * pxPaddingBetweenItem) - (int) (8 * pxPaddingInItem)) / 4;
        float imageWidth = (float) totalImageWidth;
        return imageWidth;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:56:01+00:00Added an answer on June 16, 2026 at 5:56 pm

    the problem is, that you create a scaled Bitmap by using the old big one. After that you have two Bitmaps in your Memory and you don’t even recycle the old one.

    Anyway, there is a better way:

    ImageView imageView = (ImageView) findViewById(R.id.some_id);
    String pathToImage = "path";
    
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathToImage, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;
    
    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/50, photoH/50);
    
    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;
    
    Bitmap bitmap = BitmapFactory.decodeFile(pathToFile, bmOptions);
    imageView.setImageBitmap(bitmap);
    

    Edit:

    When you want to use a resource Id instead of the file path, use decodeResource and do the last part like this:

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId, bmOptions);
    imageView.setImageBitmap(bitmap);
    

    Hope that piece of code helps you out!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble with handling errors I am curious about the exisitng error
I may just be having trouble with the error-handling documentation, but what I have
I'm having trouble handling the scenario whereby an event is being raised to a
I'm having trouble handling IDs of my databse tables using OpenJPA and HSQLdb. I
I'm building a simple interpreter in python and I'm having trouble handling differing numbers
I have an application that's having some trouble handling multi-processor systems. It's not an
I'm having trouble wrapping my head around .NET DataTable events, handling, actions, etc. I
I am having trouble with catching and gracefully handling commons fileupload's FileUploadBase.SizeLimitExceededException or spring's
I am having trouble handling the selections in DataGridView . My grid view contains
I'm having trouble handling a certain redirect with Python. I'm requesting a page that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.