2012-10-23 58 views
4

我有一個問題/問題,我一直在尋找幾個星期,仍然沒有解決方案。 這個應用程序是用它內部的框架拍照。該框架在PNG中。java.lang.OutOfMemoryError一個攝像頭應用程序

當我啓動應用程序everythong變得很好,但是當我點擊按鈕拍照時,應用程序崩潰。有時它會給OutOfMemoryError並有時會發生ArrayIndexOutOfBoundsException,所以我嘗試了幾乎所有的東西,但什麼都沒有。 我應該改變drawable的大小嗎?還是應該設置最終bmp的大小?

我很抱歉,我的英語,因爲我是法國人.. 這裏是代碼

private class CapturePictureCallback implements PictureCallback { 

    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 

     Bitmap picBitmap = BitmapFactory.decodeByteArray(data, 10, 
       data.length); 

     final ViewFlipper flipper = (ViewFlipper) findViewById(R.id.main_flipper); 
     View flipperView = flipper.getCurrentView(); 

     Bitmap flipperBitmap = Bitmap.createBitmap(flipperView.getWidth(), flipperView.getHeight(), Bitmap.Config.ARGB_4444); 
     Canvas flipperCanvas = new Canvas(flipperBitmap); 
     flipperView.draw(flipperCanvas); 
     Bitmap resizedFlipperBitmap = Bitmap.createScaledBitmap(flipperBitmap, 500, 300, true); 

     // Insert image on top 
     Bitmap overlaidBitmap = overlay(picBitmap, resizedFlipperBitmap); 

     picBitmap.recycle(); 
     resizedFlipperBitmap.recycle(); 

     // Create file 
     save(overlaidBitmap); 
     overlaidBitmap.recycle(); 
     camera.startPreview(); 
    } 

    private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) { 
     Bitmap bmOverlay = Bitmap.createBitmap(0,0, bmp1.getConfig()); 
     Canvas canvas = new Canvas(bmOverlay); 
     canvas.drawBitmap(bmp1, new Matrix(), null); 
     canvas.drawBitmap(bmp2, new Matrix(), null); 
     return bmOverlay; 
    } 


    private void save(Bitmap bitmap) { 
     File picturesFolder = null; 
     if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) { 
      picturesFolder = new File(
        Environment.getExternalStorageDirectory(), "Pictures"); 
     } else { 
      picturesFolder = Environment 
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
     } 

     Date date = new Date(System.currentTimeMillis()); 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm"); 
     String dateString = sdf.format(date); 
     String fileName = "kissthepresident" + dateString + ".jpeg"; 
     File pictureFile = new File(picturesFolder, fileName); 

     try { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      BufferedOutputStream bos = new BufferedOutputStream(fos); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bos); 
      bitmap.recycle(); 

      if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) { 
       ContentValues values = new ContentValues(7); 

       values.put(Images.Media.TITLE, fileName); 
       values.put(Images.Media.DISPLAY_NAME, fileName); 
       values.put(Images.Media.DATE_TAKEN, dateString); 
       values.put(Images.Media.MIME_TYPE, "image/jpeg"); 
       values.put(Images.Media.ORIENTATION, 0); 
       values.put(Images.Media.DATA, 
         pictureFile.toString()); 
       values.put(Images.Media.SIZE, pictureFile.length()); 

       Uri uri = Uri.fromFile(pictureFile); 
       getContentResolver().insert(uri, values); 

      } else { 
       MediaScannerConnection.scanFile(getApplicationContext(), 
         new String[] { pictureFile.toString() }, null, 
         new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(String path, Uri uri) { 
         Log.i("ExternalStorage", "Scanned " + path + ":"); 
         Log.i("ExternalStorage", "-> uri=" + uri); 
        } 
       }); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
@Override 
protected void onDestroy() { 
    super.onDestroy(); 

    unbindDrawables(findViewById(R.id.main_layout)); 
    System.gc(); 
} 

private void unbindDrawables(View view) { 
    if (view.getBackground() != null) { 
     view.getBackground().setCallback(null); 
    } 
    if (view instanceof View) { 
     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
      unbindDrawables(((ViewGroup) view).getChildAt(i)); 
     } 
     ((ViewGroup) view).removeAllViews(); 
    } 
} 

這裏是目錄下載

FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException 
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:403) 
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:418) 
at com.kissthepresidents.Main$CapturePictureCallback.onPictureTaken(Main.java:255) 
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:529) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:3701) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
at dalvik.system.NativeStart.main(Native Method) 
+1

您是否嘗試過調試您的代碼?你有一些想法可能會導致OutOfMemoryError?令人驚訝的是,很多人認爲StackOverflow的用戶喜歡免費調試其他人的代碼。 – CKing

回答

1

一個問題: decodeByteArray(字節[] data,int offset,int length)

Bitmap picBitmap = BitmapFactory.decodeByteArray(data, 10, 
      data.length); 

應該是(我不知道你的偏移量是多少,但你應該改正這個長度)。

Bitmap picBitmap = BitmapFactory.decodeByteArray(data, 10, 
      data.length - 10); 
+1

實際上,當我把0,我得到的內存錯誤,當我把10我得到另一個.. – MobiusApps