Java.lang.Runtimeexception:Takepicture失败了吗?

时间:2014-02-09 10:25:40

标签: java android camera

我正在Android App中的后台拍照。但它会出错:

02-09 15:22:12.061: E/cheeta(28633): timer testing
02-09 15:22:13.546: W/System.err(28633): java.lang.RuntimeException: takePicture failed
02-09 15:22:13.546: W/System.err(28633):    at android.hardware.Camera.native_takePicture(Native Method)
02-09 15:22:13.546: W/System.err(28633):    at android.hardware.Camera.takePicture(Camera.java:1194)
02-09 15:22:13.551: W/System.err(28633):    at cam.sharp.MainActivity$MyTimerTask.run(MainActivity.java:69)
02-09 15:22:13.551: W/System.err(28633):    at java.util.Timer$TimerImpl.run(Timer.java:284)
02-09 15:22:13.551: E/cheeta(28633): timer testing
02-09 15:22:15.051: W/System.err(28633): java.lang.RuntimeException: takePicture failed
02-09 15:22:15.051: W/System.err(28633):    at android.hardware.Camera.native_takePicture(Native Method)
02-09 15:22:15.051: W/System.err(28633):    at android.hardware.Camera.takePicture(Camera.java:1194)
02-09 15:22:15.051: W/System.err(28633):    at cam.sharp.MainActivity$MyTimerTask.run(MainActivity.java:69)
02-09 15:22:15.051: W/System.err(28633):    at java.util.Timer$TimerImpl.run(Timer.java:284)
02-09 15:22:15.051: E/cheeta(28633): timer testing
02-09 15:22:16.551: W/System.err(28633): java.lang.RuntimeException: takePicture failed
02-09 15:22:16.556: W/System.err(28633):    at android.hardware.Camera.native_takePicture(Native Method)
02-09 15:22:16.556: W/System.err(28633):    at android.hardware.Camera.takePicture(Camera.java:1194)
02-09 15:22:16.561: W/System.err(28633):    at cam.sharp.MainActivity$MyTimerTask.run(MainActivity.java:69)
02-09 15:22:16.561: W/System.err(28633):    at java.util.Timer$TimerImpl.run(Timer.java:284)
02-09 15:22:16.561: E/cheeta(28633): timer testing

我有两个文件。

MainActivity.java和CameraPreview.java

以下是两者的代码。

MainActivity.java

package cam.sharp;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private int cameraId = 0;
    private Camera mCamera;
    private CameraPreview mPreview;
    String fileName = "tempImage.jpeg";
    File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create an instance of Camera
        mCamera = getCameraInstance(cameraId);

        if (mCamera == null) {
            Toast.makeText(
                    getApplicationContext(),
                    "The camera service is currently unavailable, please try again!",
                    Toast.LENGTH_LONG).show();
            finish();
        } else {
            // Create our Preview view and set it as the content of our
            // activity.
            mPreview = new CameraPreview(this, mCamera);
            FrameLayout frameLayout = (FrameLayout) findViewById(R.id.camera_preview);
            frameLayout.addView(mPreview);

        }

        // start thread for these

        MyTimerTask myTask = new MyTimerTask();
        Timer myTimer = new Timer();
        // public void schedule (TimerTask task, long delay, long period)
        // Schedule a task for repeated fixed-delay execution after a specific
        // delay.
        //
        // Parameters
        // task the task to schedule.
        // delay amount of time in milliseconds before first execution.
        // period amount of time in milliseconds between subsequent executions.

        myTimer.schedule(myTask, 3000, 1500);

    }

    class MyTimerTask extends TimerTask {
        public void run() {

            try {
                mCamera.takePicture(null, null, null, mPictureCallback);
                file = new File(getFilesDir(), fileName);

            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.e("cheeta", "timer testing");

        }
    }

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera c) {
            Log.e("Callback TAG", "Here in jpeg Callback");

            if (imageData != null) {
                FileOutputStream outputStream;
                try {
                    outputStream = openFileOutput(fileName,
                            Context.MODE_PRIVATE);
                    outputStream.write(imageData);
                    outputStream.close();

                    // Intent intent = new Intent(SnapScreen.this,
                    // PreviewScreen.class);
                    // if (fromMessageReview == true) {
                    // intent.putExtra("fromMessageReview", "true");
                    // }
                    // startActivity(intent);
                    // overridePendingTransition(R.anim.slide_in,
                    // R.anim.slide_out);

                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseCamera();
    }

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance(int cameraId) {
        Camera c = null;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                c = Camera.open(cameraId);
            } else {
                c = Camera.open();
            }
        } catch (Exception e) {
            c = null;
        }
        return c; // returns null if camera is unavailable
    }

    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.release(); // release the camera for other applications
            mCamera = null;
        }
    }

}

CameraPreview.java

package cam.sharp;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class */
@SuppressLint("ViewConstructor")
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
    private static final String TAG = "Camera Preview";
    private SurfaceHolder mHolder;
    public Camera mCamera;

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mCamera.setDisplayOrientation(90);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the
        // preview.
        try {
                mCamera.setPreviewDisplay(holder);
                mCamera.setDisplayOrientation(90);
                mCamera.startPreview(); 

        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e) {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

任何人都可以看到问题是什么?我打电话给 mCamera.startPreview(); ,但仍无用。

由于

1 个答案:

答案 0 :(得分:6)

您的代码中有2个问题:

首先:在您的onPictureTaken回拨中,您正在调用finish()方法,而这反过来表示应该销毁该活动,并调用onDestroy()方法,然后释放你的相机。但是你的MainActivity.java没有被销毁(不确定为什么,但是通过logCat,我发现onCreate()只被调用一次,所以我认为活动没有被销毁。可能的解释可能因为Timer是由一个不同的线程控制的,因此可能不知道MainActivity被销毁,但我无法确认),所以你的myTimer将继续运行,当它到达{ {1}}它会抛出一个mCamera.takePicture(null, null, null, mPictureCallback);因为相机已经被释放了,并且没有再次调用NullPointException来获取mCamera的新实例。

所以,解决第一个问题:

MainActivity.onCreate()

第二:您在哪里调用Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { Log.e("Callback TAG", "Here in jpeg Callback"); if (imageData != null) { FileOutputStream outputStream = null; try { outputStream = openFileOutput(fileName, Context.MODE_PRIVATE); outputStream.write(imageData); // Removed the finish call you had here } catch (Exception e) { e.printStackTrace(); } finally { if (outputStream != null) try { outputStream.close(); } catch (IOException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } } } } }; 方法。根据{{​​1}}:

的文件
  

此方法仅在预览处于活动状态时有效(之后   startPreview())。拍摄图像后将停止预览;   如果要重新启动,调用者必须再次调用startPreview()   预览或拍摄更多照片。这不应该在两者之间调用   start()和stop()。

您在创建相机时只调用了startPreview()一次,并且由于问题1,MainActivity上的takePicture()仅被调用一次。由于你有一个定时器每1.5秒拍照,你应该在致电startPreview()之前致电onCreate(),以解决这个问题:

startPreview()

在此之后,应用程序继续拍照并存储它们。我从来没有使用像这样的计时器,所以我不知道如何让它停止。如果您只想在takePicture()的每次调用中拍摄少量照片,我建议您使用带有动作监听器的Timer,如下所示:

class MyTimerTask extends TimerTask {
    public void run() {

        try {
            // Call startPreview before taking a picture
            mCamera.startPreview();
            mCamera.takePicture(null, null, null, mPictureCallback);
            file = new File(getFilesDir(), fileName);
        } catch (NullPointerException ne) {
            ne.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.e("cheeta", "timer testing");

    }
}