/** Backup APK demo */
public static void backApk(Context context) {String dstPath = "/sdcard/update/";//save path dirString apkNameString = null;String packageInfo = context.getApplicationInfo().sourceDir;
// The sourcedir document describes the full path to the location of this package, full path of the package. try {packageinfo = context. getpackagemanager (). getapplicationinfo ("org. thinkinbunny ", applicationinfo. flag_installed ). sourcedir; apknamestring = context. getpackagemanager (). getapplicationinfo ("org. thinkinbunny ", applicationinfo. flag_installed ). classname;} catch (namenotfoundexception E1) {// Catch Block e1.printstacktrace ();}
String backapk = packageinfo; try {New file (dstpath). mkdirs (); If (! Dstpath. endswith ("/") dstpath = dstpath + "/"; // get the path to copy the file copyfile (new file (backapk ), new file (dstpath + "/" + apknamestring + ". APK ");} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} Private Static final int buff_size = 8192; Private Static int copyfile (File SRC, file DST) throws interruptedexception {int ret = 0; fileinputstream fin = NULL; fileoutputst Ream fout = NULL; bufferedinputstream in = NULL; bufferedoutputstream out = NULL; If (src = NULL) {return-1;} Try {fin = new fileinputstream (SRC ); fout = new fileoutputstream (DST); In = new bufferedinputstream (FIN, buff_size); out = new bufferedoutputstream (fout, buff_size); byte [] bytes = new byte [buff_size]; int length; while (length = in. read (bytes ))! =-1) {out. write (bytes, 0, length);} Out. flush ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {try {If (Fin! = NULL) Fin. Close (); If (fout! = NULL) fout. Close (); return ret;} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} return ret ;}
The Code has been written first. Recently, I have been messing up things and need to back up APK. elasticsearch knows that APK can be backed up without the need of root.
After all, pay attention to adding read and write permissions to the list file.
context.getPackageManager().getApplicationInfo("org.thinkinbunny", ApplicationInfo.FLAG_INSTALLED).sourceDir;
Org. thinkinbunny: This is the registration to be copied. As for the Dir annotation, Google will describe the source code in this way/*** full path to the location of this package. */Public String sourcedir;
As for the name to be saved, you need it.