因此,這裏是我的代碼:CreateTempFile在圖片文件夾中返回沒有這樣的文件或目錄?
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 */
);
然而,我的應用程序間歇性地在File.createTempFile
拋出錯誤:
java.io.IOException: open failed: ENOENT (No such file or directory).
從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.
什麼的可能性無法寫入臨時文件?由於我指向外部存儲器,並且我的外部SD卡已插入設備內部。
您是否在API 23(Marshmallow)設備上運行?您是否在運行時請求存儲權限? – ianhanniballake
不,我正在使用4.1.2 JB設備。順便說一下,這個應用程序僅供內部使用,因此設備已定義,並且使用棒棒糖運行的設備沒有問題。 – Rendy