我正在尝试在Android中制作文件管理器应用。
我想为用户提供移动其文件的选项。因此,首先我要复制文件,然后如果没有错误,则要删除文件。
这是我用来复制文件的代码
public static boolean copy(File copy, String directory, Context con) {
static FileInputStream inStream = null;
static OutputStream outStream = null;
DocumentFile dir = getDocumentFileIfAllowedToWrite(new File(directory), con);
String mime = "";
DocumentFile copy1 = dir.createFile(mime, copy.getName());
try {
inStream = new FileInputStream(copy);
outStream = con.getContentResolver().openOutputStream(copy1.getUri());
byte[] buffer = new byte[16384];
int bytesRead;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inStream.close();
outStream.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
但是在我的一台设备中,文件只是被删除而不进行复制。
所以我的想法是,我将通过SourceFile.length(
检查文件的长度,并检查DestinationFile.length()
的长度(如果两者相同或不同)。如果两者相同,那么我将删除SourceFile。
是不检查文件MD5的最有效方法吗?另外,文件传输不完整/损坏并且长度仍然相同的机会是什么?