在我的 android 应用程序中,我遇到了file.exists
功能问题。下面是我获取两个变量的函数。from
是文件的完整路径,并且to
是我必须复制文件的目录路径。例如
from == "/mnt/sdcard/Media/Image/Abstact wallpapers/abstraction-360x640-0033.jpg";
和
to == "/mnt/sdcard";
public static boolean copyFile(String from, String to) {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
int end = from.toString().lastIndexOf("/") - 1;
String str1 = from.toString().substring(0, end);
String str2 = from.toString().substring(end+2, from.length());
File source = new File(str1, str2);
File destination= new File(to, str2);
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
return true;
} catch (Exception e) {
return false;
}
}
当我调试它时,if (source.exists())
返回 false 但我的具有此路径的文件存在。我做错了什么?