在android中创建文件对象失败
我想通过FileOutputstream将文件写入我手机的sd卡。目录已正确创建,但文件本身的创建失败。这是我的密码:在android中创建文件对象失败,android,file-io,Android,File Io,我想通过FileOutputstream将文件写入我手机的sd卡。目录已正确创建,但文件本身的创建失败。这是我的密码: File outputDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/scan"); outputDir.mkdirs(); File outputFile = new File(outputDir, "image_" +
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();
}
}
cameraView.takePictureOutput文件;首先要拍照。然后用户可能需要确认它是否正常。那么谁来拯救它呢?在哪里?如果无法保存,则保存时会出现错误。我编辑了该问题以显示takePicture方法正在执行的操作。是否调用了onPictureTaken?有陷阱吗?您应该在其中放置几个日志语句,以了解实际发生的情况。下面是错误消息:。您仍然不知道何时以及在哪个代码行上出现该错误。/BitmapFactory24467:无法解码流:????位图工厂???代码中没有BitmapFactory。这看起来不像是储蓄。
@Override
protected String doInBackground(Bitmap... params) {
Bitmap bitmapToSave = params[0];
File fPath = getAlbumStorageDir("FileName");
File f = new File(fPath, "FileName" + Calendar.getInstance().getTimeInMillis() + ".txt");
String filePath = null;
try {
FileOutputStream strm = new FileOutputStream(f);
bitmapToSave.compress(yourCompressFormat, 100, strm);
strm.close();
filePath = f.getPath();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}