在集成tinker遇到把生成的修复包放到SD修复失败的问题,百度看了很多资料都是直接加载SD卡路径的修复包,后看文档是建议放到Dir使用目录里面 SD卡目录里面容易被删除,
创建个类把SD卡的修复包复制到Dir文件夹里面;
public class FileUtils { /** * 复制文件 * 把一个文件(sourceFile ) 拷贝到另一个文件里面(targetFile) * @param sourceFile 源文件 * @param targetFile 目标文件 * @throws IOException IO异常 */ public static void copyFile(File sourceFile, File targetFile) throws IOException { // 新建文件输入流并对它进行缓冲 FileInputStream input = new FileInputStream(sourceFile); BufferedInputStream inBuff = new BufferedInputStream(input); // 新建文件输出流并对它进行缓冲 FileOutputStream output = new FileOutputStream(targetFile); BufferedOutputStream outBuff = new BufferedOutputStream(output); // 缓冲数组 byte[] b = new byte[1024 * 5]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } // 刷新此缓冲的输出流 outBuff.flush(); // 关闭流 inBuff.close(); outBuff.close(); output.close(); input.close(); } }
//使用
//加载补丁包 File sourcesFile = new File(Environment.getExternalStorageDirectory(), "patch_signed_7zip.apk"); File privateFile = new File(getDir("odex", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + "patch_signed_7zip.apk"); //判断私有目录 是否存在 文件 if (privateFile.exists()) { privateFile.delete();//存在就删除文件 } try { //使用封装的方法 把 sd卡里面的dex文件 复制到私有目录里面 FileUtils.copyFile(sourcesFile, privateFile); Toast.makeText(this, "复制私有目录成功", Toast.LENGTH_SHORT).show(); //修复dex文件 TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), privateFile.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); }
//修复包没有放到Dir文件夹里面 就会修复失败,把修复包放到Dir就ok