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 8296209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:54:09+00:00 2026-06-08T14:54:09+00:00

How do I get a bitmap with a certain (memory friendly) size from the

  • 0

How do I get a bitmap with a certain (memory friendly) size from the camera?

I’m starting a camera intent with:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
cameraIntent.putExtra("return-data", true);

photoUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "mytmpimg.jpg"));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);        

startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);

I handle the result here:

//  Bitmap photo = (Bitmap) intent.getExtras().get("data");

Bitmap photo = getBitmap(photoUri);

Now if I use the commented line – get the bitmap directly, I get always a 160 x 120 bitmap, and that’s too small. If I load it from the URI using some standard stuff I found (method getBitmap), it loads a 2560 x 1920 bitmap (!) and that consumes almost 20 mb memory.

How do I load let’s say 480 x 800 (the same size the camera preview shows me)?

Without having to load the 2560 x 1920 into memory and scaling down.

  • 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-08T14:54:11+00:00Added an answer on June 8, 2026 at 2:54 pm

    Here is what I came up with, based on a method called getBitmap() from a crop library which was removed from old Android version. I did some modifications:

    private Bitmap getBitmap(Uri uri, int width, int height) {
        InputStream in = null;
        try {
            int IMAGE_MAX_SIZE = Math.max(width, height);
            in = getContentResolver().openInputStream(uri);
    
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
    
            BitmapFactory.decodeStream(in, null, o);
            in.close();
    
            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }
    
            //adjust sample size such that the image is bigger than the result
            scale -= 1;
    
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            in = getContentResolver().openInputStream(uri);
            Bitmap b = BitmapFactory.decodeStream(in, null, o2);
            in.close();
    
            //scale bitmap to desired size
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, width, height, false);
    
            //free memory
            b.recycle();
    
            return scaledBitmap;
    
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
        return null;
    }
    

    What this does is load the bitmap using BitmapFactory.Options() + some sample size – this way the original image is not loaded into memory. The problem is that the sample size just works in steps. I get the “min” sample size for my image using some maths I copied – and subtract 1 in order to get the sample size which will produce the min. bitmap bigger than the size I need.

    And then in order to get the bitmap with exactly the size requested do normal scaling with Bitmap.createScaledBitmap(b, width, height, false);. And immediatly after it recycle the bigger bitmap. This is important, because, for example, in my case, in order to get 480 x 800 bitmap, the bigger bitmap was 1280 x 960 and that occupies 4.6mb memory.

    A more memory friendly way would be to not adjust scale, so a smaller bitmap will be scaled up to match the required size. But this will reduce the quality of the image.

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

Sidebar

Related Questions

Bitmap bmp = intent.getExtras().get(data); int size = bmp.getRowBytes() * bmp.getHeight(); ByteBuffer b = ByteBuffer.allocate(size);
I am using following code to get bitmap from url. This function is used
I am using following method to get bitmap from url but image is not
I want to get the Bitmap from URL like - https://graph.facebook.com/100003506521332/picture I tried how-i-can-display-images-from-url-in-blackberry
I use the line below to get thumbnail: bitmap = MediaStore.Images.Thumbnails.getThumbnail(act.getApplicationContext().getContentResolver(), fid, MediaStore.Images.Thumbnails.MICRO_KIND, null);
How would I get the copyright date from a bitmap? private void toolStripMenuItemLoadImage_Click(object sender,
Once in a while i get the error message Bitmap size exceeds VM budget.
When you get a bitmap from a resource for example with: Bitmap src =
I have tried to get the bitmap image from the path of the image.
I am wondering how to get the bitmap from specific location as follows: try

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.