2014-01-23 67 views
1

程序目標:用戶可以在相機預覽的頂部放置一張圖片並自由移動(圖像大1920×1080像素),並可以存儲這兩層(圖片和相機預覽)按下照片按鈕。到目前爲止,他只能保存一張這樣的照片,因爲當他拿第二張照片出現內存不足錯誤問題時。相機預覽內存不足錯誤

解決方案看來,如果您不再需要使用位圖時使用回收位圖功能,則問題可以解決。或者你可以調整它們(不想)......這些好主意嗎?

代碼:

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

回答

3

,當你遇到內存不足錯誤,您可以使用android:largeHeap="true"上AndroidManifest應用標籤內。

+0

我會試一試,謝謝^^ – pb77