About Android How to install the APK under the assets file

Source: Internet
Author: User

Install the APK file in the assets directory in your app


public class Mainactivity extends Activity {Context mcontext; @Overrideprotected void OnCreate (Bundle savedinstancestate ) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mcontext = This;//Toast.makeText ( This, "" +environment.getexternalstoragedirectory (). GetAbsolutePath (), 0). Show (); if (Copyapkfromassets (This, " Test.apk ", Environment.getexternalstoragedirectory (). GetAbsolutePath () +"/test.apk ")) {Builder m = new Alertdialog.builder (Mcontext). SetIcon (R.drawable.ic_launcher). Setmessage ("Are you installing? "). SetIcon (R.drawable.ic_launcher). Setpositivebutton (" Yes ", new Onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Intent Intent = new Intent (Intent.action_view); Intent.addflags (intent.flag_ Activity_new_task); Intent.setdataandtype (Uri.parse ("file://" + environment.getexternalstoragedirectory (). GetAbsolutePath () + "/test.apk"), "application/vnd.android.package-archive"); mcontext.startactivity (intent);}}); M.show ();}} public Boolean COPYAPKFRomassets (context context, string fileName, String path) {Boolean copyisfinish = false; try {InputStream is = context.ge Tassets (). open (FileName); File File = new file (path); File.createnewfile (); FileOutputStream fos = new FileOutputStream (file); byte[] temp = new byte[1024]; int i = 0; while ((i = is.read (temp)) > 0) {fos.write (temp, 0, i);} fos.close (); Is.close (); Copyisfinish = true; } catch (IOException e) {e.printstacktrace ();} return copyisfinish; }}


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.