android picture copy widget

I used java to make a gadget for android image copying, do it yourself, and have enough food and clothing.

reason

After making a picture with sketch, the exported picture is in a folder named as follows:

  1. [email protected]
  2. [email protected]
  3. [email protected]
  4. [email protected]

The Android image structure is probably like this:

res

  • drawable-mdpi
  • drawable-hdpi
  • drawable-xhdpi
  • drawable-xxhdpi

That is to say, when copying pictures, they need to be placed one by one, which is more troublesome.

as the picture shows:

img

code

So use java to write a jar to automatically complete.

code show as below:

package com.qefee.pj.drawablecopy;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

public class Application {
    public static void main(String[] args) {

        if (args.length >= 2) {
            String drawableFolderString = args[0];
            File drawableFolder = new File(drawableFolderString);

            String toDirString = args[1];

            File toDir = new File(toDirString);

            FileFilter fileFilter = new FileFilter() {
                public boolean accept(File pathname) {
                    boolean pngFlag = pathname.getName().endsWith(".png");
                    boolean jpgFlag = pathname.getName().endsWith(".jpg");
                    return pngFlag || jpgFlag;
                }
            };
            DrawableCopyWalker directoryWalker = new DrawableCopyWalker(fileFilter, toDir);
            try {
                directoryWalker.start(drawableFolder);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            echo("参数错误");
        }
    }

    private static void echo(String msg) {
        System.out.println(msg);
    }
}
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";

        // app icon
        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);
    }

}

maven file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qefee.pj</groupId>
    <artifactId>TestMaven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>

    </dependencies>

</project>

Instructions

After exporting the jar, execute it on the command line

Among them, fromFolderis the picture folder, toFolderpointing to the res folder of Android

java -jar copyImage.jar fromFolder toFolder(res)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568162&siteId=291194637