我正在使用下面的代碼在Android中移動文件。在Android中移動文件時出錯
public static void moveFile(String inputPath, String inputFile, String outputPath, String absolutePath) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file
out.flush();
out.close();
out = null;
// delete the original file
new File(absolutePath).delete();
}
catch (FileNotFoundException fnfe1) {
Log.e("Moving file not found", fnfe1.getMessage());
}
catch (Exception e) {
Log.e("While Moving", e.getMessage());
}
但大多數的時候,它表明了我的錯誤,如下圖所示:
移動找不到文件:/call_14-43-46_IN_+919737276726.amr(只讀文件系統)
我已選中並且要移動的文件是普通文件,因爲它是由呼叫記錄器記錄而不是隻讀的。 請幫忙。
告訴你如何調用這個函數。顯示參數的值。 – greenapps
'dir.mkdirs();'。檢查返回值,因爲它可能無法創建目錄。在這種情況下,向用戶表示敬酒。並返回。不要繼續,因爲嘗試在不存在的目錄中創建文件是沒有意義的。 – greenapps