将数据从表面视图转换为Jpeg

时间:2014-10-18 14:42:01

标签: android bitmap out-of-memory bitmapimage

您好我从表面视图捕获帧并让任务将其转换为jpg并保存到文件。但我不能释放记忆。几秒钟后,我有OutOfMemoryExc。这里是我的异步:

    @Override
        protected Void doInBackground(byte[]... frames) {

            byte[] data = frames[0];

            int[] imagePixels = convertYUV420_NV21toRGB8888(data, mPreviewWidth, mPreviewHeight);
            Bitmap bitmapImage = Bitmap.createBitmap(imagePixels, mPreviewWidth, mPreviewHeight, Bitmap.Config.ARGB_8888);
            bitmapImage.recycle();
            ByteArrayOutputStream byteArrayOs = new ByteArrayOutputStream();
            bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOs);

            bitmapImage.recycle();
            System.gc();
            OutputStream os = null;

            boolean imageSaved = true;

            try {
                os = new FileOutputStream(FileService.generateFrameFile());
                os.write(byteArrayOs.toByteArray());
            } catch (Exception e) {
                e.printStackTrace();
                imageSaved = false;
            } finally {
                try {
                    if(bitmapImage != null) {
                        bitmapImage.recycle();
                    }
                    if(byteArrayOs != null) {
                        byteArrayOs.close();
                    }
                    if (os != null) {
                        os.flush();
                        os.close();
                    }
                } catch (Exception e) {
                    imageSaved = false;
                }
            }
            mCallback.onTaskCompleted(imageSaved);

            return null;
        }

0 个答案:

没有答案