Android:从assets资源目录下安装apk

来源:互联网 发布:java导出excel的方法 编辑:程序博客网 时间:2024/05/21 17:57

原文链接: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.     }  

0 0