1

I am developing an android application in which I have to upload image from sdcard to internet. For this I am using MediaStore content provider and getting the image path from MediaStore.Images.Media.DATA column and now what I am doing is that creating a bitmap from that path using Bitmap.decodeFile(String path) method and then displaying that bitmap in a ImageView in a List. The problem I am facing is when I try to go to the activity which is showing this image in list I am getting an error like this 10-20 11:10:47.070: ERROR/AndroidRuntime(922): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

Can anybody suggest me solution to this problem or an alternative way to achieve my goal. Thanks in advance.

Rocker
  • 669
  • 5
  • 15
  • it is better to use the resize of image code before set the image to imageview – Pinki Oct 20 '11 at 06:02
  • what you mean I am not able to get you. Please throw some more light on this comment. – Rocker Oct 20 '11 at 06:04
  • resize the selected image . when u resize the image it reduces the size to avoid out of memory Exception – Pinki Oct 20 '11 at 06:07
  • Your bitmap is using too much memory so resize it for that search on google using OOM you will get thousands of post http://stackoverflow.com/questions/6213690/outofmemory-error-while-joining-large-images – ingsaurabh Oct 20 '11 at 06:08
  • what is the size of image file? – user370305 Oct 20 '11 at 06:09

3 Answers3

1

This happens many time cause the image stored in the sdcard are of high resolution and the your app can't handle the size of image into heap size

After Using the below line to get Bitmap

Bitmap bmp = Bitmap.decodeFile(String path) 

You should try scaling it down to your required size

newBmp = Bitmap.createScaledBitmap(bmp, 240, 320, true);

and then use the newBmp to set in your view.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
1

Below code is used to resize the selected image

Bitmap bitmap  = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int newWidth = 100;
            int newHeight = 100;

            // calculate the scale - in this case = 0.4f
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;

            // createa matrix for the manipulation
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(scaleWidth, scaleHeight);

            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
                    width, height, matrix, true); 

            ((ImageView) findViewById(R.id.ImageviewTest)).setImageBitmap(resizedBitmap);
Pinki
  • 21,723
  • 16
  • 55
  • 88
0
String filepath = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    File file = new File(filepath, filename);
    FileInputStream fs;
    try {
        fs = new FileInputStream(file);
        BitmapFactory.Options bfOptions = new BitmapFactory.Options();
        /*
         * bfOptions.inDither=false; //Disable Dithering mode
         * bfOptions.inPurgeable=true; //Tell to gc that whether it needs
         * free memory, the Bitmap can be cleared
         * bfOptions.inInputShareable=true;
         */
        bfOptions.inJustDecodeBounds = false;
        bfOptions.inTempStorage = new byte[32 * 1024];
        Bitmap b = BitmapFactory.decodeFileDescriptor(fs.getFD(), null,
                bfOptions);
        img.setImageBitmap(b);
}
catch()
{
}
himanshu
  • 97
  • 1
  • 5