Android OutOfMemoryException用相机拍照

时间:2013-07-04 08:19:20

标签: android bitmap camera out-of-memory

我正在尝试拍照并在其上添加叠加层。这是我的代码(只有回调):

 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());
            } 

        }
    };

我的Samsung Galaxy S3(android 4.1.2)出现以下错误,出现OutOfMemoryException

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

奇怪的是,它在三星Gio(android 2.2.1)上运行完美,分辨率为1600x1200。

我google了很多,我无法使用缩小图片的主要解决方案。这是一个内存问题,但我不知道如何减少内存使用量。

编辑:我发现了这一点,似乎是真的:https://stackoverflow.com/a/12377158/1343969

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

您应该只需调用Canvas的空构造函数并在其上绘制photo。这将删除copy()调用并减少内存使用量。

photo = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap cadre = BitmapFactory.decodeResource(getResources(), R.drawable.cadre16001200);
Canvas canvas = new Canvas();
canvas.drawBitmap(photo, 0, 0, null);
canvas.drawBitmap(cadre, new Matrix(), null);
cadre.recycle();

photo.compress(CompressFormat.JPEG, 100, fos);