相机预览内存不足错误

时间:2014-01-23 04:33:06

标签: android memory camera overlay preview

该程序的目标:用户可以在相机预览的顶部放置一张图片并自由移动(图像大于1920x1080像素)并可以存储这两层(图片和相机)预览)按下照片按钮。到目前为止,他只能保存一张这种图片,因为当他拿第二张图片时会出现内存不足错误问题。

解决方案如果您在不再需要使用位图时使用循环位图功能,似乎可以解决问题。或者你可以调整它们的大小(不想)......这些好主意是什么?

代码:

private PictureCallback mPicture = new PictureCallback()
{
    @Override
    public void onPictureTaken(byte[] data, Camera camera)
    {   
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        mPreview.setDrawingCacheEnabled(true);
        mPreview.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
        Bitmap bitmap = mPreview.getDrawingCache();
        bmp = BitmapFactory.decodeByteArray(data, 0, data.length);  
        combination = overlay(bmp, bitmap);

        if(pictureFile == null)
        {
            return;
        }
        try
        {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            combination.compress(CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "MyCameraApp");
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                    Uri.parse("file://"+ mediaStorageDir)));
        }
        catch(FileNotFoundException e)
        {
            Log.d(TAG, "File not found: "+e.getMessage());
        }
        catch(IOException e)
        {
            Log.d(TAG, "Error accessing file: "+e.getMessage());
        }
    }

};

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{   
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(),bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, null, new Rect(0,0,bmp1.getWidth(), bmp1.getHeight()), null);
    return bmOverlay;       
}

private static File getOutputMediaFile(int type)
{
    File mediaFile = null; 

    if(isSdPresent() == false)
    {
        Log.d(TAG, "There is no Sd card. Cannot use the camera");
    }

   else
    {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "World Cup Camera");

        if(!mediaStorageDir.exists())
        {
            if(!mediaStorageDir.mkdirs())
            {
                Log.d("WorldCupApp", "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());       

        if (type == MEDIA_TYPE_IMAGE)
        {        
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");    
        }

        else 
        {       
            return null;  
        }           
    }
    return mediaFile;
}

我应该使用这个代码吗?

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

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } 
    catch (FileNotFoundException e) {}
    return null;
}

1 个答案:

答案 0 :(得分:3)

当您遇到内存错误时,您可以在AndroidManifest上的应用标记内使用android:largeHeap="true"