1

我是相机内功能的新手。当我启动相机并单击图像视图上设置的图像后,然后单击效果按钮。现在应用程序已关闭。错误是内存。这个应用程序是在 2.2 以下版本上制作的。当我曾经在 4.4 版本中运行此应用程序时,该应用程序被弯腰显示应用程序中的内存泄漏

12-05 17:10:53.898: E/dalvikvm-heap(21667): Out of memory on a 31961104-byte allocation.
12-05 17:10:53.938: E/dalvikvm(21667): Out of memory: Heap Size=42403KB, Allocated=34174KB, Limit=49152KB
12-05 17:10:53.938: E/dalvikvm(21667): Extra info: Footprint=42403KB, Allowed Footprint=42403KB, Trimmed=0KB
12-05 17:10:53.948: E/AndroidRuntime(21667): FATAL EXCEPTION: main
12-05 17:10:53.948: E/AndroidRuntime(21667): java.lang.OutOfMemoryError: (Heap Size=42403KB, Allocated=34174KB)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.graphics.Bitmap.nativeCreate(Native Method)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.graphics.Bitmap.createBitmap(Bitmap.java:917)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.graphics.Bitmap.createBitmap(Bitmap.java:897)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at com.photomarging.PhotoMargingActivity.createSepiaToningEffect(PhotoMargingActivity.java:577)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at com.photomarging.PhotoMargingActivity.applyEffect(PhotoMargingActivity.java:472)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at com.photomarging.PhotoMargingActivity$5.onClick(PhotoMargingActivity.java:199)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.view.View.performClick(View.java:3549)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.view.View$PerformClick.run(View.java:14393)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.os.Handler.handleCallback(Handler.java:605)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.os.Looper.loop(Looper.java:154)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at android.app.ActivityThread.main(ActivityThread.java:4945)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at java.lang.reflect.Method.invokeNative(Native Method)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at java.lang.reflect.Method.invoke(Method.java:511)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-05 17:10:53.948: E/AndroidRuntime(21667):    at dalvik.system.NativeStart.main(Native Method)

主要课程在这里http://pastebin.com/sR5FPG22另一个课程在这里http://pastebin.com/GY3xzPBW(照片合并),我不明白如何解决问题帮助我和预览课程在这里

    @TargetApi(8)
  class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";

SurfaceHolder mHolder;
public Camera camera;

Preview(Context context) {
    super(context);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    if(camera==null){
    camera = Camera.open();
    }
    else{
        try {
            camera.reconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        camera.setPreviewDisplay(holder);


        camera.setPreviewCallback(new PreviewCallback() {

            public void onPreviewFrame(byte[] data, Camera arg1) {
                FileOutputStream outStream = null;
                try {

                    ByteArrayInputStream imagestream=new     
  ByteArrayInputStream(data);
                    Bitmap     
      icon=BitmapFactory.decodeStream(imagestream);

    PhotoMargingActivity.img.setImageBitmap(icon);
                    outStream = new 
       FileOutputStream(String.format("/sdcard/%d.jpg",    
  System.currentTimeMillis())); 
                    outStream.write(data);
                    outStream.close();
                    Log.d(TAG, "onPreviewFrame - wrote bytes: 
         " + data.length);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
                    Preview.this.invalidate();
                    //
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // Surface will be destroyed when we return, so stop the preview.
    // Because the CameraDevice object is not a shared resource, it's very
    // important to release it when the activity is paused.
    camera.stopPreview();

    camera.release();
    camera = null;
}
     ///////////////////// change in the preve class /////////////////////////////
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Now that the size is known, set up the camera parameters and begin
    // the preview.
     //        Camera.Parameters parameters = camera.getParameters();
      //        parameters.setPreviewSize(w, h);
      //        camera.setParameters(parameters);
      //        camera.startPreview();

    // Now that the size is known, set up the camera parameters and begin
    // the preview.
    //        Camera.Parameters parameters = camera.getParameters();
     //        parameters.setPreviewSize(w, h);
     //        camera.setParameters(parameters);
      //        camera.startPreview();

     Camera.Parameters parameters = camera.getParameters();
        List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

        // You need to choose the most appropriate previewSize for your app
        Camera.Size previewSize;//= // .... select one of previewSizes here
                 if(previewSizes.size() > 1){
                        previewSize =previewSizes.get(1);  // .... select one of    
           previewSizes here
                    }else{
                        previewSize =previewSizes.get(0);
                    }
        parameters.setPreviewSize(previewSize.width, previewSize.height);
        camera.setParameters(parameters);
        camera.startPreview();

}

@Override
public void draw(Canvas canvas) {
        super.draw(canvas);
        Paint p= new Paint(Color.RED);
        Log.d(TAG,"draw");
        canvas.drawText("PREVIEW", canvas.getWidth()/2, canvas.getHeight()/2, p );
}
4

1 回答 1

0

在您处理使用中的主要变量的特定位置,在您的代码中使用以下代码。

Runtime.getRuntime().gc();

另外,请继续练习,通过以下方式回收不需要的位图对象。

if (bitmap != null){
  bitmap.recycle();
  bitmap = null;
}

希望这对您有所帮助。

谢谢。

于 2012-12-05T12:35:44.827 回答