image file
内有一个directory
。如何copy
将此image file
转换为刚刚创建的另一个directory
?这两个directories
位于设备的internal storage
上:)
答案 0 :(得分:13)
您可以使用这些功能。如果传入文件,第一个将复制包含所有子项的整个目录或单个文件。第二个只对文件有用,并且在第一个文件中为每个文件调用。
另请注意,您需要具有执行此操作的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
功能:
public static void copyFileOrDirectory(String srcDir, String dstDir) {
try {
File src = new File(srcDir);
File dst = new File(dstDir, src.getName());
if (src.isDirectory()) {
String files[] = src.list();
int filesLength = files.length;
for (int i = 0; i < filesLength; i++) {
String src1 = (new File(src, files[i]).getPath());
String dst1 = dst.getPath();
copyFileOrDirectory(src1, dst1);
}
} else {
copyFile(src, dst);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(File sourceFile, File destFile) throws IOException {
if (!destFile.getParentFile().exists())
destFile.getParentFile().mkdirs();
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
答案 1 :(得分:5)
如果要以编程方式复制图像,请使用以下代码。
File sourceLocation= new File (sourcepath);
File targetLocation= new File (targetpath);
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
答案 2 :(得分:0)
**使用FileUtils这是一种简单快捷的最佳方法,并从此处下载Jar文件**
public void MoveFiles(String sourcepath){
Exception in Tkinter callback
Traceback (most recent call last):
File
"C:\Users\samue\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py",
line 1705, in __call__
return self.func(*args)
File "C:\Users\samue\OneDrive - The Becket
School\Airport_Tkinters_GUI_29.12.18.py", line 485, in cost
date_price_increase()
File "C:\Users\samue\OneDrive - The Becket
School\Airport_Tkinters_GUI_29.12.18.py", line 325, in date_price_increase
tickets_per_person = tickets_per_person * 1.15
UnboundLocalError: local variable 'tickets_per_person' referenced before
assignment