Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 拍摄照片,然后显示出来_Java_Android_Android Intent - Fatal编程技术网

Java 拍摄照片,然后显示出来

Java 拍摄照片,然后显示出来,java,android,android-intent,Java,Android,Android Intent,我正在尝试构建一个应用程序,当按下一个按钮时,相机就会启动,用户会拍摄一张照片,然后显示在我的应用程序中。我遇到的问题是,在onActivityResult中,resultCode是-1,intent是null。我已经把我的代码建立起来了 我的代码如下所示: private static final int CAMERA_PIC_REQUEST = 1337; private String mCurrentPhotoPath; public void cameraClick(View view

我正在尝试构建一个应用程序,当按下一个按钮时,相机就会启动,用户会拍摄一张照片,然后显示在我的应用程序中。我遇到的问题是,在onActivityResult中,resultCode是-1,intent是null。我已经把我的代码建立起来了

我的代码如下所示:

private static final int CAMERA_PIC_REQUEST = 1337;
private String mCurrentPhotoPath;

public void cameraClick(View view){
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if(cameraIntent.resolveActivity(getPackageManager()) != null){
        File photo = null;
        try{
            photo = createImageFile();
        }
        catch (IOException e){
            e.printStackTrace();
        }

        if(photo != null){
            //cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    }


}

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
   if(requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
     //do image setting here
   }
}

你在舱单上有摄像头权限吗?android.permission。CAMERA@njzk2我在拍照后按下复选标记。@RandyFreak我确实已声明相机许可。我的错-1确实可以。因此,您没有收到任何数据的原因可能是因为您的设备要求您在触发相机之前指定图像的路径。请参见,数据可能不是空的,但data.getData是空的