0

私の質問は古いかもしれませんが、過去 2 週間のこの問題のために苦しんでおり、今では多すぎます。私のアプリケーションでは、大きな画像ビットマップをサーバーに送信する必要があり、以下のコーディングでこれを行っています。

  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;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
                bfOptions.inTempStorage=new byte[32 * 1024];


                File file=new File(TabGroupActivity.path);
                FileInputStream input=null;
                try {
                    input = new FileInputStream(TabGroupActivity.path);
                } catch (FileNotFoundException e) {
                    //TODO do something intelligent
                    e.printStackTrace();
                }
                if(input!=null)
                {

                bitmap = BitmapFactory.decodeStream(input, null, bfOptions);

                }

                  Matrix mat = new Matrix();//removing rotations

                   if(TabGroupActivity.rotation==90 ||  TabGroupActivity.rotation==270)
                    {
                        mat.setRotate(90);
                    }
                    else if(TabGroupActivity.rotation==0 || TabGroupActivity.rotation==180)
                    {
                        mat.setRotate(0);
                    }
                        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);

                       ByteArrayOutputStream bos = new ByteArrayOutputStream();

                int height= bitmap.getHeight();
                int width=bitmap.getWidth();

                     {
                //formula for calculating aspect ratio 

        float k= (float) height/width;
         newHeight =Math.round(620*k);
             }


        byte[] buffer=new byte[10];

        while(input.read(buffer)!=-1)
        {
             bos.write(buffer);
        }

                bitmap = Bitmap.createScaledBitmap(bitmap , 620, newHeight, true);
                bitmap.compress(CompressFormat.JPEG, 90, bos);
                byte[] imageData = bos.toByteArray();
                ContentBody cb = new ByteArrayBody(imageData, "image/jpg", "image1.jpeg");

                  input.close();

しかし、2、3枚の画像を送信した後、メモリ不足のエラーが発生しましBitmapFactory.decodeStream()た。この問題を解決するのを手伝ってください。画像のサイズを変更したりトリミングしたりできない主なものです。良質の画像をサーバーにのみ送信する必要があります。

4

1 に答える 1

0

を呼び出して、ビットマップを使い終わるたびにビットマップをリサイクルしますbitmap.recycle()。これは少し役立つはずです。また、コードを少し最適化します。この部分:

           if(TabGroupActivity.rotation==90 ||  TabGroupActivity.rotation==270)
            {
                mat.setRotate(90);
            }
            else if(TabGroupActivity.rotation==0 || TabGroupActivity.rotation==180)
            {
                mat.setRotate(0);
            }
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);

に減らすことができます

           if(TabGroupActivity.rotation==90 ||  TabGroupActivity.rotation==270)
            {
                mat.setRotate(90);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);

            }

したがって、すべての場合に新しいビットマップを作成する必要はありません。

また、 を試してBitmapFactory.Options.inSampleSize小さいイメージを取得することもできます。

于 2012-06-18T10:08:23.013 に答える