0

写真を撮り、その上にオーバーレイを追加しようとしています。これが私のコードです(コールバックのみ):

 private PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(final byte[] data, Camera camera) {


            if(!dirFile.exists()){
                dirFile.mkdirs();
            }

            try {
                String name = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date()) + ".jpg";
                picturePath = new File(dirFile, name);

                new AsyncTask<Void, Void, Void>(){
                    FileOutputStream fos = new FileOutputStream(picturePath);
                    Bitmap photo;

                    @Override
                    protected Void doInBackground(Void... params) {
                        photo = BitmapFactory.decodeByteArray(data, 0, data.length).copy(Config.ARGB_8888, true);
                        Bitmap cadre = BitmapFactory.decodeResource(getResources(), R.drawable.cadre16001200);
                        Canvas canvas = new Canvas(photo);
                        canvas.drawBitmap(cadre, new Matrix(), null);
                        cadre.recycle();

                        photo.compress(CompressFormat.JPEG, 100, fos);
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                       geotag(picturePath.toString());
                        return null;
                    }

                    protected void onPostExecute(Void result) {

                        dialog.dismiss();

                        mCamera.startPreview();


                        //Affiche la nouvelle photo
                        picture.setImageBitmap(photo);
                    };

                }.execute();


            } catch (FileNotFoundException e) {
                Log.d("PhotoActivity", "File not found: " + e.getMessage());
            } 

        }
    };

OutOfMemoryException を使用して、Samsung Galaxy S3 (android 4.1.2) で次のエラーが発生します。

07-04 10:01:24.076: E/dalvikvm-heap(2980): Out of memory on a 7680016-byte allocation.

奇妙なことに、同じ解像度の 1600x1200 の Samsung Gio (android 2.2.1) で完全に動作します。

私はたくさんグーグルで検索しましたが、画像を縮小するという主な解決策を使用できません。これはメモリの問題ですが、メモリ使用量を減らす方法がわかりません。

編集:私はこれを見つけました、それは本当の問題だったようです:https://stackoverflow.com/a/12377158/1343969

4

2 に答える 2

1

UI に処理する前にビットマップをデコードする必要があります。コード例を次に示します。

private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            FileInputStream stream1=new FileInputStream(f);
            BitmapFactory.decodeStream(stream1,null,o);
            stream1.close();

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            FileInputStream stream2=new FileInputStream(f);
            Bitmap bitmap=BitmapFactory.decodeStream(stream2, null, o2);
            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e) {
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
}
于 2013-07-04T08:24:39.430 に答える