从文件源解码和旋转位图时出现OutofMemoryException

时间:2014-09-21 21:52:48

标签: android

我正在尝试从文件源中设置我的应用程序的SetBackGround,计算宽度和高度,然后根据需要使用此代码旋转它:

    public Bitmap decodeFile(File input){
            Bitmap bmpCompressed = null;
            try {
                //Decode image size

                BitmapFactory.Options o11 = new BitmapFactory.Options();
                o11.inJustDecodeBounds = true;
                o11.inPurgeable = true;
                o11.inDither = false;
                o11.inInputShareable = true;
                o11.inTempStorage = new byte[32 * 1024];  
                BitmapFactory.decodeStream(new FileInputStream(input),null,o11);

                //The new size we want to scale to
                final int REQUIRED_SIZE=getDimensions.HEIGHT;

            //   int width = getDimensions.WIDTH;

                //Find the correct scale value. It should be the power of 2.
                int scale=1;


 o22.inSampleSize=calculateInSampleSize(o11,getDimensions.WIDTH,getDimensions.HEIGHT);
                BitmapFactory.decodeStream(new FileInputStream(input), null, o22);
                bmpCompressed = BitmapFactory.decodeFile(input.getAbsolutePath(), o22);

               Log.i("BG","Scale : "+scale);
                ExifInterface exif = new ExifInterface(input.getAbsolutePath());

               int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

               if(orientation != ExifInterface.ORIENTATION_NORMAL){

                   int rotatesize = 0;

                   switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotatesize = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotatesize = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotatesize = 270;
                    break;

                default:
                    break;
                }

               Matrix matrix = new Matrix();
               matrix.postRotate(rotatesize);
               bmpCompressed = Bitmap.createBitmap(bmpCompressed, 0, 0, bmpCompressed.getWidth(), bmpCompressed.getHeight(), matrix, true);

               }
                //    FileOutputStream out = null;
                try {
             //      out = new FileOutputStream(file);
              //     bmpCompressed.compress(CompressFormat.JPEG, 100, out); 

                } catch (Exception e) {
                e.printStackTrace();
                } finally {
                   try{
                 //      out.close();
                   } catch(Throwable ignore) {}
                }

               } catch (IOException e) {}
              return bmpCompressed;


  public static int calculateInSampleSize(
                    BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {

                // Calculate ratios of height and width to requested height and width
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);

                // Choose the smallest ratio as inSampleSize value, this will guarantee
                // a final image with both dimensions larger than or equal to the
                // requested height and width.
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }



            return inSampleSize;
        }

我在backGround中这样做,图像大小是6mb,但我仍然得到OutofMemorryException,我该如何解决这个问题?

0 个答案:

没有答案