尝试使用以下代码将文件从一个目录复制到另一个目录并重命名
ClassLoader
它似乎不会复制文件。在这种情况下,路径Path3在SDCard上,但我也需要它在设备上从一个目录复制到另一个目录
基本上我使用图库选取器从某个地方挑选图像我将uri转换为路径然后需要将文件从存储位置复制到图片目录并重命名
知道我哪里出错了吗?
答案 0 :(得分:0)
我解决了它从各种各样的来源以错误的方式进行。本守则有效:
File sourceLocation = new File (Path2);
File targetLocation = new File (Path3 + "/" + imageFileName);
Log.v(TAG, "sourceLocation: " + sourceLocation);
Log.v(TAG, "targetLocation: " + targetLocation);
try {
int actionChoice = 2;
if(actionChoice==1){
if(sourceLocation.renameTo(targetLocation)){
Log.v(TAG, "Move file successful.");
}else{
Log.v(TAG, "Move file failed.");
}
}
else{
if(sourceLocation.exists()){
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Log.v(TAG, "Copy file successful.");
}else{
Log.v(TAG, "Copy file failed. Source file missing.");
}
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}