Automatically compile .9.png files

Source: Internet
Author: User

When you open the file with the image browsing tool, you can see that the image is surrounded by a circle of single pixels, and the black pixel is that we use draw9patch. after we put the image in the Res/drawable folder and compile it on Android, decompress the APK package. You can see that although the Res/logs file is actually usableProgramTo simulate this process, you can directly import the .9.png image, and then automatically generate the edited .9.png image.

Upload images, and finally Delete the generated temporary files.

The program source code is as follows:

Import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import Java. util. enumeration; import Java. util. iterator; import Java. util. map; import java.util.zip. zipentry; import java.util.zip. zipfile; import javax. swing. filechooser. filesystemview; public class compile {Private Static string ninepngpath = ""; Private Static string command; Private Static string desktop = filesystemview. getfilesystemview (). gethomedirectory (). getabsolutepath (); Private Static file tempfile = new file (desktop + "\ Temp "); // After the copy is successful, delete the private static file compiledfile = new file (desktop + "\ compiled"); // Delete the public static void main (string [] ARGs) after the copy is successful) {If (! Accessaapt () {system. Out. println ("cannot access the aapt command! "); Return;} If (ARGs. length = 0) {} else {ninepngpath = ARGs [0];} File Manifest = new file ("androidmanifest. XML "); file Android = new file (" android. jar "); file resource = new file (" res "); file ninepngfile = new file (ninepngpath); try {If (ninepngpath. trim (). length () = 0 |! Ninepngfile. exists () {system. Out. println ("the parameter you entered is incorrect! ");} Else {// copy the .9.png file to the copyfile (ninepngfile, resource. getabsolutepath () + "\ drawable \" + ninepngfile. getname (); command = "aapt"; command + = "package"; command + = "-M" + manifest. getabsolutepath (); command + = "-s" + resource. getabsolutepath (); command + = "-I" + android. getabsolutepath (); command + = "-F" + desktop + "\ Temp" contains invalid runtime.getruntime(cmd.exe C (command); thread. sleep (3000); If (tempfil E. exists () {unzip (tempfile. getabsolutepath (), compiledfile. getabsolutepath (); file srcfile = new file (compiledfile. getabsolutepath () + "\ res \ drawable \" + ninepngfile. getname (); copyfile (srcfile, desktop + "\" + ninepngfile. getname ();} else {system. out. println ("zip file not obtained");} catch (exception e) {system. out. println ("command execution error! "); E. printstacktrace ();} system. Out. println (" file output to desktop! "); // Delete the temporary file deletetempfile (tempfile); // runtime.getruntime(cmd.exe C (" "); deletetempfile (compiledfile );}

// Decompress the ZIP file. Just extract the desired .9.png file to Private Static void unzip (string zipfile, string destdir) {destdir = destdir. endswith ("\\")? Destdir: destdir + "\"; // system. out. println (destdir); byte B [] = new byte [1024]; int length; zipfile; try {zipfile = new zipfile (new file (zipfile )); enumeration enumeration = zipfile. entries (); zipentry = NULL; while (enumeration. hasmoreelements () {zipentry = (zipentry) enumeration. nextelement (); If (! Zipentry. getname (). endswith ("PNG") {continue;} file LoadFile = new file (destdir + zipentry. getname (); // system. out. println (":" + LoadFile. getabsolutepath (); If (zipentry. isdirectory () {// This section can be avoided, because each time it seems to traverse the LoadFile from the bottom layer. mkdirs ();} else {If (! LoadFile. getparentfile (). exists () LoadFile. getparentfile (). mkdirs (); outputstream = new fileoutputstream (LoadFile); inputstream = zipfile. getinputstream (zipentry); While (length = inputstream. read (B)> 0) outputstream. write (B, 0, length); inputstream. close (); outputstream. close () ;}} zipfile. close (); system. out. println ("the file has been decompressed! ");} Catch (ioexception e) {e. printstacktrace () ;}// test whether aapt can be accessed in the system. EXT file. Make sure to configure the environment variable Private Static Boolean accessaapt () {Map <string, string> envs = system. getenv (); iterator <map. entry <string, string> iter = envs. entryset (). iterator (); While (ITER. hasnext () {map. entry <string, string> entry = ITER. next (); string value = entry. getvalue (); string [] values = value. split (";"); For (INT I = 0; I <values. lengt H; I ++) {string STR = values [I]; file F = new file (STR); If (F. isdirectory () {string [] filenames = f. list (); For (Int J = 0; j <filenames. length; j ++) {If (filenames [J]. trim (). equals ("aapt.exe") {return true ;}}}return false;} Private Static Boolean copyfile (File srcfile, string descpath) {try {fileinputstream FCM = new fileinputstream (srcfile); file DESC = new file (descpath); If (! Desc. exists () {DESC. createnewfile ();} fileoutputstream Fos = new fileoutputstream (DESC); byte [] buffer = new byte [1024]; int length = 0; while (length = FCM. read (buffer ))! =-1) {FOS. write (buffer, 0, length);} FOS. flush (); FOS. close (); FCM. close (); Fos = NULL; FCM = NULL; system. out. println ("Copied successfully ~! "); // Srcfile. delete (); Return true;} catch (exception e) {e. printstacktrace ();} return false;} Private Static void deletetempfile (File file) {If (! File. canwrite () Try {thread. sleep (1, 2000); system. out. println ("waiting for two seconds");} catch (interruptedexception E1) {e1.printstacktrace ();} If (! File. exists () {system. Out. println ("the file does not exist anymore! ");} Else {If (file. isfile () {// file. delete (); try restarting runtime.getruntime(cmd.exe C ("CMD/C del/Q" + file. getabsolutepath ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} else {try again runtime.getruntime(cmd.exe C ("CMD/c rd/S/Q" + file. getabsolutepath ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} system. out. println ("Temporary File deleted successfully! ");}}

In addition, it should be noted that in order to make aapt. EXT works normally and needs to manually construct its execution parameters. Because the aapt tool needs to load android. jar and androidmanifest. XML, so you can consider placing them in the package. below is the directory structure of the above program:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.