Ren*_*ndy 6 android android-file
所以这是我的代码:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
Run Code Online (Sandbox Code Playgroud)
但是,我的应用程序间歇性地抛出错误File.createTempFile:
java.io.IOException: open failed: ENOENT (No such file or directory).
Run Code Online (Sandbox Code Playgroud)
来自以下文件File:
Parameters:
prefix the prefix to the temp file name.
suffix the suffix to the temp file name.
directory the location to which the temp file is to be written, or null for the default location for temporary files, which is taken from the "java.io.tmpdir" system property. It may be necessary to set this property to an existing, writable directory for this method to work properly.
Returns:
the temporary file.
Throws:
IllegalArgumentException - if the length of prefix is less than 3.
IOException - if an error occurs when writing the file.
Run Code Online (Sandbox Code Playgroud)
写入临时文件失败的可能性有多大?由于我指向外部存储器,我的外部SD卡插在设备内部.
getExternalStoragePublicDirectory()的 javadoc特别指出:
该目录可能尚不存在,因此您必须在使用它之前确保它存在,例如使用 File.mkdirs()。
之后,我认为您还应该检查该目录是否存在。
| 归档时间: |
|
| 查看次数: |
1907 次 |
| 最近记录: |