私のAndroidアプリケーションでは、file.exists
機能に問題があります。以下は、2 つの変数を取得する関数です。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 が返されますが、このパスのファイルが存在します。私が間違っていることは何ですか?