“简单”拍照不起作用

时间:2013-05-08 11:45:44

标签: android android-camera

为图像捕获意图添加额外文件路径会导致相机应用程序在具有库存系统版本4.2.1的TF300t Android平板电脑上出现故障。按“完成”按钮不执行任何操作 - 甚至不关闭相机应用程序活动。没有结果返回。

我正在使用的代码是从Adroid developers site

中提取的
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imageFile = createImageFile();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
startActivityForResult(cameraIntent, THIS_CAMERA_REQUEST);

createImageFile()定义为:

private File createImageFile() throws IOException {
    File outputDir = getBaseContext().getCacheDir();

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "photo_" + timeStamp + "_";
    File image = new File(outputDir, imageFileName);

    return image;
}

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));

已删除,相机应用按预期运行。

有没有合理的解决方法?我不想自己制作相机应用程序来拍照。

1 个答案:

答案 0 :(得分:4)

有问题的一行:

File outputDir = getBaseContext().getCacheDir();

我已将其替换为:

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "1mind_" + timeStamp + ".jpg";
    File photo = new File(Environment.getExternalStorageDirectory(),  imageFileName);
    return photo;
}

原来,图像必须存储在外部存储器中,而不是存储在缓存目录中。