我正在尝试使用我的应用拍摄照片并将其保存在文件夹中,但由于某种原因,该文件无效。它创建得很好,但它只是一个空文件(据我所知)。
我的代码:
private File createImageFile() throws IOException {
// Create an image file name
//String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String imageFileName = "SCB_";
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +
File.separator + "/SCBimages/";
File storageDir = new File(path);
storageDir.mkdirs();
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = 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) {
// Error occurred while creating the File
System.out.println(ex.getMessage());
System.out.println(Arrays.toString(ex.getStackTrace()));
Toast.makeText(getApplicationContext(), "Failed" + ex.getMessage(), Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra("", Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
奇怪的是,我的手机确实拍了照片,但它以另一个名字保存在DCMI文件夹中。有没有人知道问题是什么?
答案 0 :(得分:1)
你错过了意图中的MediaStore.EXTRA_OUTPUT。我想这一行:
takePictureIntent.putExtra("", Uri.fromFile(photoFile));
应该是
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));