1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| package com.qefee.pj.drawablecopy;
import org.apache.commons.io.DirectoryWalker; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils;
import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.ArrayList; import java.util.Collection;
public class DrawableCopyWalker extends DirectoryWalker {
private File toDir;
DrawableCopyWalker(FileFilter filter, File toDir) { super(filter, -1);
this.toDir = toDir; }
@Override protected void handleFile(File file, int depth, Collection results) throws IOException { super.handleFile(file, depth, results);
String fileName = file.getName();
String mdpi = "@mdpi"; String hdpi = "@hdpi"; String xhdpi = "@xhdpi"; String xxhdpi = "@xxhdpi"; String xxxhdpi = "@xxxhdpi";
if (fileName.contains("ic_launcher")) { if (fileName.contains(mdpi)) { copyImage(file, fileName, mdpi, toDir, "mipmap-mdpi"); } else if (fileName.contains(hdpi)) { copyImage(file, fileName, hdpi, toDir, "mipmap-hdpi"); } else if (fileName.contains(xhdpi)) { copyImage(file, fileName, xhdpi, toDir, "mipmap-xhdpi"); } else if (fileName.contains(xxhdpi)) { copyImage(file, fileName, xxhdpi, toDir, "mipmap-xxhdpi"); } else if (fileName.contains(xxxhdpi)) { copyImage(file, fileName, xxxhdpi, toDir, "mipmap-xxxhdpi"); } else { System.out.println("文件未处理 : " + file.getAbsolutePath()); } } else { if (fileName.contains(mdpi)) { copyImage(file, fileName, mdpi, toDir, "drawable-mdpi"); } else if (fileName.contains(hdpi)) { copyImage(file, fileName, hdpi, toDir, "drawable-hdpi"); } else if (fileName.contains(xhdpi)) { copyImage(file, fileName, xhdpi, toDir, "drawable-xhdpi"); } else if (fileName.contains(xxhdpi)) { copyImage(file, fileName, xxhdpi, toDir, "drawable-xxhdpi"); } else { System.out.println("文件未处理 : " + file.getAbsolutePath()); } } }
private void copyImage(File file, String fileName, String mdpi, File toDir, String child) throws IOException { File mdpiDir = new File(toDir, child); String newFileName = StringUtils.remove(fileName, mdpi); File newFile = new File(mdpiDir, newFileName); FileUtils.copyFile(file, newFile); }
void start(File startDirectory) throws IOException { ArrayList<File> dirs = new ArrayList<File>(); this.walk(startDirectory, dirs); }
}
|