0

我对拍照并保存的代码有疑问。当我启动Camera()时它崩溃了。你能帮我吗?

    private void launchCamera() {
     try {

         mOutputFile = File.createTempFile("prova", null);

          Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

           intentCamera.putExtra(MediaStore.EXTRA_OUTPUT,
                 Uri.fromFile(mOutputFile));

           startActivityForResult(intentCamera, CAMERA_REQUEST);
     } catch (Exception e) {
         Toast t = Toast.makeText(this, "ERROR:\n" + e.toString(), Toast.LENGTH_LONG);
         t.show();
     }
}
4

3 回答 3

1

我正在使用这段代码尝试一下:

/**
 * This method is used to start the camera activity and save the image taken as the imagename passed 
 * 
 * @param imagename : this is the name of the image which will be saved 
 */
private void clickPicture(String imagename) {
    Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
    File cameraFolder;
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"myfolder/");
    else
        cameraFolder= context.getCacheDir();
    if(!cameraFolder.exists())
        cameraFolder.mkdirs();
    String imageFileName = imagename;
    File photo = new File(Environment.getExternalStorageDirectory(), "myfolder/" + imageFileName);
    getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    Uri.fromFile(photo);
    startActivityForResult(getCameraImage, 1);

}

并在清单文件中添加许可:

 <uses-permission android:name="android.permission.CAMERA" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-06-25T12:13:11.333 回答
0

也许您没有在清单文件中添加必要的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

另外,我建议您阅读我写的关于使用内置相机活动拍照的主题的这篇博文:

使用内置相机活动拍照

于 2013-06-25T11:58:24.377 回答
0

要访问设备摄像头,您必须在 Android Manifest 中声明 CAMERA 权限

于 2013-06-25T12:01:32.053 回答