Android:從assets資源目錄下安裝apk

原文鏈接:http://blog.csdn.net/annkie/article/details/8150807


爲了實現將第三方apk內置在assets資源目錄下,再進行安裝的目的。


首先將需要安裝的apk複製到assets目錄下,後綴名改爲.mp3或其他免壓縮的格式。


測試代碼如下:

[java] view plaincopy
  1. public class MainActivity extends Activity  
  2. {  
  3.     private static final String TAG = "ExtractIconFromApk";  
  4.   
  5.     public void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.   
  10.         AssetManager assets = getAssets();  
  11.         try  
  12.         {  
  13.             //獲取assets資源目錄下的himarket.mp3,實際上是himarket.apk,爲了避免被編譯壓縮,修改後綴名。  
  14.             InputStream stream = assets.open("himarket.mp3");  
  15.             if(stream==null)  
  16.             {  
  17.                 Log.v(TAG,"no file");  
  18.                 return;  
  19.             }  
  20.               
  21.             String folder = "/mnt/sdcard/sm/";  
  22.             File f=new File(folder);  
  23.             if(!f.exists())  
  24.             {  
  25.                 f.mkdir();  
  26.             }  
  27.             String apkPath = "/mnt/sdcard/sm/test.apk";  
  28.             File file = new File(apkPath);  
  29.             //創建apk文件  
  30.             file.createNewFile();  
  31.             //將資源中的文件重寫到sdcard中  
  32.             //<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  33.             writeStreamToFile(stream, file);  
  34.             //安裝apk  
  35.             //<uses-permission android:name="android.permission.INSTALL_PACKAGES" />            
  36.             installApk(apkPath);  
  37.         }  
  38.         catch (IOException e)  
  39.         {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.         }         
  43.     }  
  44.   
  45.     private void writeStreamToFile(InputStream stream, File file)  
  46.     {  
  47.         try  
  48.         {  
  49.             //  
  50.             OutputStream output = null;  
  51.             try  
  52.             {  
  53.                 output = new FileOutputStream(file);  
  54.             }  
  55.             catch (FileNotFoundException e1)  
  56.             {  
  57.                 // TODO Auto-generated catch block  
  58.                 e1.printStackTrace();  
  59.             }  
  60.             try  
  61.             {  
  62.                 try  
  63.                 {  
  64.                     final byte[] buffer = new byte[1024];  
  65.                     int read;  
  66.   
  67.                     while ((read = stream.read(buffer)) != -1)  
  68.                         output.write(buffer, 0, read);  
  69.   
  70.                     output.flush();  
  71.                 }  
  72.                 finally  
  73.                 {  
  74.                     output.close();  
  75.                 }  
  76.             }  
  77.             catch (Exception e)  
  78.             {  
  79.                 e.printStackTrace();  
  80.             }  
  81.         }  
  82.         finally  
  83.         {  
  84.             try  
  85.             {  
  86.                 stream.close();  
  87.             }  
  88.             catch (IOException e)  
  89.             {  
  90.                 // TODO Auto-generated catch block  
  91.                 e.printStackTrace();  
  92.             }  
  93.         }  
  94.     }  
  95.   
  96.     private void installApk(String apkPath)  
  97.     {  
  98.         Log.v(TAG,apkPath);  
  99.           
  100.         Intent intent = new Intent(Intent.ACTION_VIEW);  
  101.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  102.         intent.setDataAndType(Uri.fromFile(new File(apkPath)),  
  103.                 "application/vnd.android.package-archive");  
  104.         startActivity(intent);  
  105.     }  

發佈了59 篇原創文章 · 獲贊 27 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章