读取asserts 文件中的apk

读取asserts 文件中的apk

任务需要,需要读取asserts文件夹中的apk文件并读取其版本信息和大小,完成后整理如下:

学习借鉴的网址:

1、https://blog.csdn.net/w0100746363/article/details/7276928

2、https://blog.csdn.net/fengyuzhengfan/article/details/38360017

 

 

/**

* 筛选asserts文件夹中的apk文件并获取需要更新安装的apk文件的相应信息

**/

private void getApkData() throws IOException {

     String[] files = null;

     Log.i("silentChange", " ------------------assertmanager");

     // 遍历assest文件夹,读取压缩包及安装包

     try {

          files = getAssets().list("");

     } catch (IOException e) {

          e.printStackTrace();

     }

     /** 遍历asserts文件夹中所有文件 **/

     if (files.length > 0) {

          for (int i = 0; i < files.length; i++) {

               String fileName = files[i];

               Log.i("silentChange", " fileName: " + fileName);

               /** 筛选asserts文件夹中的apk文件 **/

               if (fileName.contains(".apk")) {//判断是否为Apk

                    String apkName = fileName;

                    String name = apkName.substring(0, apkName.length() - 4);//去除了后缀".apk"的文件名,目前没用到

                    Log.i("silentChange", " name: " + name);

                     //不能直接读取assets目录下的Apk信息,所以需要先将其拷贝出来

                    String s = String.valueOf(this.getCacheDir().getAbsolutePath() + "/");

                    Log.i("silentChange", " s is " + s);

                    final String apkcachePath = (new StringBuilder(s)).append(apkName).toString();// 临时文件"/temp.apk"

                    /** 获取asserts文件夹中对应apk文件的信息 **/

                    AssetManager assetManager = getAssets();

                    InputStream fIn = assetManager.open(apkName);

                    newCopyFile(fIn, apkcachePath);//将apk文件拷贝到临时文件夹中

                    String apkSize = “”;

 

                    PackageManager pm = this.getPackageManager();

                    PackageInfo packageInfo = pm.getPackageArchiveInfo(apkcachePath, PackageManager.GET_ACTIVITIES);

                    ApplicationInfo applicationInfo = packageInfo.applicationInfo;

 

                    String apkPackageName = applicationInfo.packageName;//读取Apk包名

                    Log.i("silentChange", "---------apkPackageName is " + apkPackageName);

                    String versionCode = String.valueOf(packageInfo.versionCode);//读取apk版本号

                    Log.i("silentChange", " versionCode:" + versionCode);

                    Log.i("silentChange", " apkName:" + apkName);//读取apk名

                    File file = new File(apkcachePath);

                    if(file.exists()){

                         apkSize = String.valueOf(file.length());

                    }else {

                    Log.i("88","file is not exit!!!");

                    }

                    Log.i("silentChange", " apkSize:" + apkSize);

               }

         }

}

 

//将assert文件夹中的apk文件拷贝到本地缓存 另一种方法

private void newCopyFile(InputStream inputStream, String paramString) throws IOException {

     Log.i("silentChange", "--------newCopyFile"+"\n"+"paramString is: "+paramString);

     FileOutputStream fileOutputStream = null;

     try {

          fileOutputStream = new FileOutputStream(paramString);

          byte[] buffer = new byte[1024];

          int byteRead;

          while (-1 != (byteRead = inputStream.read(buffer))) {

               fileOutputStream.write(buffer, 0, byteRead);

          }

     }catch (Exception e) {

          e.printStackTrace();

     }finally {

          File file = new File(paramString);

          if (file.exists()) {

               fileLength = String.valueOf(file.length());

               Log.i("silentChange", " localFile.length() is --" + fileLength);

           }else {

          Log.i("silentChange", " file is not exit");

          }

     inputStream.close();

     fileOutputStream.flush();

     fileOutputStream.close();

     }

}

 

 

 

 

你可能感兴趣的:(读取asserts 文件中的apk)