Wednesday, July 29, 2015

Programatically install apk from assets folder in android

First use this

<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"  
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"  
  android:layout_height="match_parent" 
  android:paddingLeft="@dimen/activity_horizontal_margin"  
  android:paddingRight="@dimen/activity_horizontal_margin"  
  android:paddingTop="@dimen/activity_vertical_margin"  
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context=".MainActivity">

 <TextView android:text="@string/hello_world" 
  android:layout_width="wrap_content"      
  android:layout_height="wrap_content" />

  <Button android:layout_width="wrap_content"   
    android:layout_height="wrap_content"      
    android:text="New Button"       
    android:id="@+id/button"      
    android:onClick="Installed_APK"    
    android:layout_centerVertical="true"   
    android:layout_centerHorizontal="true" />
</RelativeLayout>



Second use this in Activity class
public void Installed_APK(View view ){
AssetManager assetManager = getAssets(); 
InputStream in = null; OutputStream out = null;   try { in = assetManager.open("JellyJumpers.apk");   out = new FileOutputStream("/sdcard/JellyJumpers.apk");   byte[] buffer = new byte[1024];   int read;   while((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close();   in = null;   out.flush();   out.close(); out = null;   Intent intent = new Intent(Intent.ACTION_VIEW);   intent.setDataAndType(Uri.fromFile(new File("/sdcard/JellyJumpers.apk")),"application/vnd.android.package-archive");  
startActivity(intent);   } catch(Exception e) { } }
Finally use this in manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

No comments:

Post a Comment