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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:14:20+00:00 2026-05-25T21:14:20+00:00

I am trying to add images in Sqlite DB and listing the images from

  • 0

I am trying to add images in Sqlite DB and listing the images from DB to listview…. I am storing the image path to get image.
When I list the images from DB in device I am getting error like java.lang.OutOfMemoryError: bitmap size exceeds VM budget

Do I clear heap memory every time.
How to correct this.. Here is my code.

LView.class

mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();                
mAlbum = (ImageView) findViewById(R.id.iv);
mAlbum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK, 
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMG);
}
});

Button click = (Button) findViewById(R.id.button);
click.setOnClickListener(new OnClickListener() {
@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mySQLiteAdapter.insert(str);
        System.out.println(str+" Added");
        Intent intent = new Intent(LView.this, MyList.class);
        startActivity(intent);              
    }
});`

`

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {

    case 1: 
        if(resultCode == RESULT_OK) {
            Uri selectedUri = data.getData();
            str = getPath(selectedUri);
            Bitmap bitmap;              
            try {                   
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedUri));
                mAlbum.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
            }
        }
}
// Converting image path Uri to String path to store in DB
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cur = managedQuery(uri, projection, null, null, null);
        int columnIndex = cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cur.moveToFirst();

        return cur.getString(columnIndex);
    }`

Pls suggest me…
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-05-25T21:14:21+00:00Added an answer on May 25, 2026 at 9:14 pm

    1) try to decode the image bound first

    you get the actual size of the image to prevent the OOM issue. Dont ever decode the image first!!!

        BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    
    BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options);'
    

    options.outWidth and options.outHeight are what you want.

    2) compute a sample size

    by using the following code from http://hi-android.info/src/com/android/camera/Util.java.html. If you detect the outWidth and outHeight are too big to have OOM issue, just set the outWidth and outHeight to a smaller size. It will give you a sample size that the decoded image will be set to those outWidth and outHeight. The bitmap options here is the same you use in step 1.

        private static int computeInitialSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
    
        int lowerBound = (maxNumOfPixels == IImage.UNCONSTRAINED) ? 1 :
                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == IImage.UNCONSTRAINED) ? 128 :
                (int) Math.min(Math.floor(w / minSideLength),
                Math.floor(h / minSideLength));
    
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
    
        if ((maxNumOfPixels == IImage.UNCONSTRAINED) &&
                (minSideLength == IImage.UNCONSTRAINED)) {
            return 1;
        } else if (minSideLength == IImage.UNCONSTRAINED) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
    

    3) Use the computed sample size

    Once you get the sample size, use it to decode the real image data.

                options.inTempStorage = new byte[16*1024];
            options.inPreferredConfig = (config == null)?BitmapUtil.DEFAULT_CONFIG:config;
            options.inSampleSize = BitmapUtil.computeSampleSize(bitmapWidth, bitmapHeight, bitmapWidth < bitmapHeight?targetHeight:targetWidth, bitmapWidth < bitmapHeight?targetWidth:targetHeight, 1);
            options.inPurgeable = true;  
            options.inInputShareable = true;  
            options.inJustDecodeBounds = false;
            options.inDither = true;
            result = BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options);
    

    ~If this still give you OOM issue, you can try to lower the outWidth and outHeight.
    bitmap is using native heap, not the java heap. So it is hard to detect how much memory left for the new image decoded.


    ~~If you have to set the outWidth and outHeight too low, then you may have memory leak somewhere in your code. try to release any object from memory that you dont use.
    e.g bitmap.release();


    ~~~the above is just a sample code. adjust what you need.

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

Sidebar

Related Questions

I am trying to add an image to a list of images when a
Hi i'm trying to add an image to an ImageView from a URL i
I'm getting this error when trying to add a foreign key contraint: #1005 -
Greetings Problem When i'm trying to add images to my silverlight project I get
I am trying to get images from Internet. (i can't show my code here)
I am trying to add Images from websites and I am facing poblem with
I m trying to store and retrieve image and text from sqlite database..My saveDatabase
I am trying to get the the bilde() methode add images too my JLabel
I'm trying to add caption of images on top of the image. These images
I'm trying to add images to my models in my Django app. models.py class

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.