8

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

我使用的代码是从Adroid 开发者网站中提取的

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));

被删除,相机应用程序按预期运行。

有没有合理的解决方法?我宁愿不自己构建相机应用程序只是为了拍照。

4

1 回答 1

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;
}

事实证明,图像必须存储在外部存储而不是缓存目录中。

于 2013-05-21T11:08:45.683 回答