在android中创建文件对象失败

时间:2015-09-09 10:15:49

标签: android file-io

我想通过FileOutputstream将文件写入手机的SD卡。目录已正确创建,但文件本身的创建失败。这是我的代码:

File outputDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/scan");
            outputDir.mkdirs();
            File outputFile = new File(outputDir, "image_" + System.currentTimeMillis() + ".jpg");
            cameraView.setResolution(MAX_HEIGHT, MAX_WIDTH);
            cameraView.takePicture(outputFile);

outputDir是可读写的,当然也设置了写入外部存储的权限。当我检查outputfile.exists()时,结果为false。这是错误消息:

09-09 12:05:56.232: E/BitmapFactory(24467): Unable to decode stream: java.io.FileNotFoundException: /storage/sdcard0/scan/image_1441793154138.jpg: open failed: ENOENT (No such file or directory)

我在这里做错了什么?

编辑: 以下是cameraView对象在拍摄图片时所做的事情:

public void takePicture(File file) {
    this.file = file;
    mCamera.setPreviewCallback(null);

    mCamera.takePicture(null, null, this);
}

@Override
public void onPictureTaken(byte[] data,
        Camera camera) {
    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

    try {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data);
        fos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

AndroidManifest.xml