我正在尝试构建一个应用程序,当按下某个按钮时,会启动相机意图,然后用户拍摄一张照片,然后显示在我的应用中。我遇到的问题是onActivityResult
resultCode
为-1,意图为空。我已将代码设置为this sample from google
我的代码如下所示:
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
}
}