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

  • Home
  • SEARCH
  • 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 8229793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:54:57+00:00 2026-06-07T16:54:57+00:00

I am trying to store a bitmap (that i have previously read from a

  • 0

I am trying to store a bitmap (that i have previously read from a file) after decoding it in a preferable size using BitmapFactory.Options.inSampleSize. The problem is that the file size of the stored bitmap is at least double the size of the original file. I have searched a lot and could not find how i can deal with this, since i don’t want it to happen for memmory efficiency (later i reuse the stored bitmap). Here is my method that does what i describe:

private Bitmap decodeFileToPreferredSize(File f) {
    Bitmap b = null;
    try {
        // Decode image size
        Log.i("Bitmap", "Imported image size: " + f.length() + " bytes");
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(f.getAbsolutePath(), o);

        //Check if the user has defined custom image size
        int scale = 1;
        if(pref_height != -1 && pref_width != -1) {

            if (o.outHeight > pref_height || o.outWidth > pref_width) {

                scale = (int) Math.pow(
                        2,
                        (int) Math.round(Math.log(pref_width
                                / (double) Math.max(o.outHeight, o.outWidth))
                                / Math.log(0.5)));
            }
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        b = BitmapFactory.decodeFile(f.getAbsolutePath(), o2);
        String name = "Image_" + System.currentTimeMillis() + ".jpg";
        File file = new File(Environment.getExternalStorageDirectory(), name);
        FileOutputStream out = new FileOutputStream(file);
        b.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.close();
        Log.i("Bitmap", "Exported image size: " + file.length() + " bytes");

    } catch (Exception e) {
        b = null;
    }

    return b;
}

UPDATE I saw the densities on the two image files. The one that came from camera intent has 72 dpi density for width and height. The image file created from my above method has 96 dpi density for width and height. This explains why a 0.5 MByte image that came form the camera is resized in approximatelly 2.5 MByte with my above method since the rescale factor is (96/72) * 2 ~= 2.5. For some reason, the bitmap i create does not take the density of the image that came from the camera. I tried to set the density with all variation of BitmapFactory.Options.inDensity but no luck. Also i tried to change the bitmap density with bitmap.setDensity(int dpi); but still no effect. So, my new question is if there is a way to define the density of the bitmap when the image is stored.

Thanks in advance.

  • 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-07T16:54:59+00:00Added an answer on June 7, 2026 at 4:54 pm

    I had a similar issue. When I downloaded images from web they used more space on the SD than they did when downloaded to my PC from browser. I think the issue is simply that BitmapFactory saves the images in a non optimzed format of some sort.

    My workaround was to use following instead of the bitmapfactory:

        try {
            try {
                is = yourinputstream;
                // Consider reading stream twice and return the bitmap.
    
                os = youroutputstream;
    
                byte data[] = new byte[4096];
                int count;
                while ((count = is.read(data)) != -1) {
                    os.write(data, 0, count);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(is != null) {
                    is.close();
                }
                if(os != null) {
                    os.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to store BitmapData from a MovieClip that is a PNG with transparency.
I am trying to serialize a Bitmap to store in an XML file. My
I'm trying to store MediumBlob data in MySQL using java. But I have no
I am trying to store the links that I scrape from a site in
Im trying to create a Bitmap from a Png file stored on the SD
I am trying to store a byteArrayInputStream as File on a FTP Server .
I'm trying to store an image into an SQL database without using temporary files.
I'm trying to store the size of a queue in an int in C++.
I'm trying to implement the following: I have an Items Manager, that has an
I am trying to download a set of .jpg's from Amazon S3 and store

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.