Java.lang.Runtime异常:拍照失败了吗?

And*_*eta 11 java camera android

我正在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
Run Code Online (Sandbox Code Playgroud)

我有两个文件.

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

}
Run Code Online (Sandbox Code Playgroud)

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到问题是什么?我正在调用mCamera.startPreview(); 但仍然没用.

谢谢

ped*_*mss 6

您的代码中有2个问题:

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

那么,解决第一个问题:

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

        }
    }
};
Run Code Online (Sandbox Code Playgroud)

第二:你在哪里称呼你的startPreview()方法.根据以下文件takePicture():

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

startPreview()在创建相机时,您只调用一次,并且由于问题1,onCreate()仅在主要活动上调用一次.由于你有一个计时器每1.5秒拍照,你应该在打电话startPreview()之前打电话takePicture(),所以要解决这个问题:

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");

    }
}
Run Code Online (Sandbox Code Playgroud)

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

Timer tm = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // Take x pictures
            tm.stop();
        }
    });
Run Code Online (Sandbox Code Playgroud)