从相机拍照

时间:2013-06-25 11:54:53

标签: android android-intent

我拍摄照片并保存代码有问题。它在我启动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();
     }
}

3 个答案:

答案 0 :(得分: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" />

答案 1 :(得分: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" />

另外我建议你回顾一下这篇关于使用内置相机活动拍照的主题的博文:

Take Picture with build in Camera Activity

答案 2 :(得分:0)

要访问设备相机,您必须在Android Manifest中声明CAMERA权限