Android backup APK

Source: Internet
Author: User
 
 
 
 
 
/** 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.

Related Article

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.