我在尝试拍照并从onActivityResult()获取它时,在我的android应用程序中遇到了一些问题。我想确保代码可以正常工作,因此我刚刚复制了此页面的确切代码http://developer.android.com/training/camera/photobasics.html。拍照android
所以这些是我使用的方法;我叫dispatchTakePictureIntent()时,在button.`
private File createImageFile() throws IOException {
// Create an image file name
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;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, 15);
}
}
}`
问题的用户点击的是,当onActivityResult()方法被调用时,我得到正确的requestCode,右resultCode为,但第三个参数为空。我想获取图像位图或路径,但是我不能没有它。为什么会发生?是否因为这段代码不应该返回该参数?在那种情况下,我应该如何修改它?
你的路径已经是:如果你想收到预览图像从您的代码删除此行'mCurrentPhotoPath' – njzk2 2015-01-26 18:21:11
是的,我尝试过使用这条道路,但它没有工作,这是因为它被定义为“file:”+ image.getAbsolutePaht();我删除了“file:”,现在它工作 – Mantech 2015-01-26 19:18:26