我知道有很多问题和我的问题相似。但是这个问题不同。我使用以下方法将文件从文件夹A复制到文件夹B,使用的是EXTERNAL_STORAGE
:
public static String copyFile(String path) {
String fileToName = String.valueOf(System.currentTimeMillis());
File pathFrom = new File(path);
File pathTo = new File(Environment.getExternalStorageDirectory() + "/.noname");
File file = new File(pathTo, fileToName + ".bak");
while (file.exists()) {
fileToName = String.valueOf(System.currentTimeMillis());
file = new File(pathTo, fileToName + ".bak");
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(pathFrom);
out = new FileOutputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
out.write(data);
in.close();
out.close();
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return file.getPath();
}
路径参数为:"/storage/emulated/0/Download/image_preview.jpg"。 执行此方法时,我遇到了一个错误:
/storage/emulated/0/Download/tree_leaves_sunlight.jpg: open failed: ENOENT (No such file or directory)
。
文件夹.noname
已经存在。
对于我的问题,有什么建议吗?**更新:我使用
ImageView
打开此文件。当我不打开它时,我可以复制它。但是当我打开时,我遇到了这个错误。
PS:我在ImageView
中预览图像。并且有一个Button
复制图像。单击按钮执行将此图像复制到其他文件夹的方法。
ImageView
打开了这个文件。 - Phan SinhImageView
中预览图像。还有一个复制图像的Button
。当点击Button
时,执行将此图像复制到其他文件夹的方法。 - Phan Sinh