为什么我的应用拍摄的照片在Android中为0尺寸?

时间:2014-09-14 20:22:27

标签: android media

我的应用程序在Windows资源管理器中拍摄的图片始终为0字节,无法打开。我只能在Android Gallery应用中看到它们。

我正在用这种方法拍照:

private void takePhoto() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = PhotoUtils.createImageFile(this);
        } catch (IOException ex) {
            Log.v("MainActivity", "Creating image exception");
        }
        if (photoFile != null) {
            fileUri = Uri.fromFile(photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

这是PhotoUtils.createImageFile(...)

public static File createImageFile(Context context) throws IOException {
    String currentPhotoPath;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "ES_" + timeStamp;

    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);

    currentPhotoPath = image.getAbsolutePath();
    galleryAddPic(context, currentPhotoPath);
    return image;
}

public static void galleryAddPic(Context context, String currentPhotoPath) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(currentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        context.sendBroadcast(mediaScanIntent);
    }

1 个答案:

答案 0 :(得分:1)

当文件为临时文件时,它以零字节长度为索引编制索引。尝试移动galleryAddPic()电话,直到拍完照片为止。