0

I am getting out of memory error:

Below is my code:

public static Bitmap decodeSampledBitmapFromResource(InputStream inputStream,int reqWidth, int reqHeight) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            try {
            while ((len = inputStream.read(buffer)) > -1 ) {
               baos.write(buffer, 0, len);
            }
            baos.flush();
            InputStream is1 = new ByteArrayInputStream(baos.toByteArray()); 
            InputStream is2 = new ByteArrayInputStream(baos.toByteArray()); 

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither=false;      
        options.inPurgeable=true;                   
        options.inInputShareable=true; 
        options.inJustDecodeBounds = true;
      //  BitmapFactory.decodeResource(res, resId, options);
        BitmapFactory.decodeStream(is1,null,options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeStream(is2,null,options); // error at this line
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

i am getting error at this line

BitmapFactory.decodeStream(is2,null,options);

I get this error Out of memory on a 3250016-byte allocation.

i have seen many post on this but still unable to find a solution in this case.

Goofy
  • 6,098
  • 17
  • 90
  • 156
  • what error you are getting? please paste the LogCat or StackTrace. – Qadir Hussain Mar 06 '13 at 12:41
  • Why you loading the stream to the memory first ? From where you getting the Image from server or from a local file ? – Triode Mar 06 '13 at 12:41
  • I have folder called data i am getting the imges from that using classloader which returns me an input stream and i am passing that to this method. – Goofy Mar 06 '13 at 12:43
  • @QadirHussain please see i have added in my question – Goofy Mar 06 '13 at 12:44
  • @Goofy, check this, this solution works perfectly for me - http://stackoverflow.com/a/15380872/1433187 – Khobaib Jul 18 '13 at 09:27

1 Answers1

2

please don't make this function static there are memory issues with the static members.

second You ain't using BitmapFactory.decodeStream(is1,null,options); anywhere so remove this from your code.

also try this code I use it to get the bitmap from the sdcard path you can easily modify it to suit your needs, I use it to deal with the similar situations and it never fails.

public Bitmap getImage(String path) throws IOException
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);        
        int srcWidth = options.outWidth;
        int srcHeight = options.outHeight;
        int[] newWH =  new int[2];
        newWH[0] = srcWidth/2;
        newWH[1] = (newWH[0]*srcHeight)/srcWidth;

        int inSampleSize = 1;
        while(srcWidth / 2 >= newWH[0]){
            srcWidth /= 2;
            srcHeight /= 2;
            inSampleSize *= 2;
        }

        //      float desiredScale = (float) newWH[0] / srcWidth;
        // Decode with inSampleSize
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inSampleSize = inSampleSize;
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options);
        ExifInterface exif = new ExifInterface(path);
        String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s);
        Matrix matrix = new Matrix();
        float rotation = rotationForImage(Add_View_Images_Activity.this, Uri.fromFile(new File(path)));
        if (rotation != 0f) {
            matrix.preRotate(rotation);
        }
        int newh = ( w * sampledSrcBitmap.getHeight() ) /sampledSrcBitmap.getWidth();
        Bitmap r=Bitmap.createScaledBitmap(sampledSrcBitmap, w, newh, true);
        Bitmap resizedBitmap = Bitmap.createBitmap(
                r, 0, 0, w, newh, matrix, true);

        return resizedBitmap;
    }
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37