android中H5本地缓存加载优化的方法
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了android中H5本地缓存加载优化的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
动手: 1.用 filedownloader 去下载了Html压缩文件
implementation 'com.liulishuo.filedownloader:library:1.7.7'
封装下载工具类:
public class FileDownloadUtils {public static FileDownloadUtils instance = null;public FileDownloadUtils() {}public static FileDownloadUtils getInstance() { if (null == instance) { instance = new FileDownloadUtils(); } return instance;}public void startDownLoadFileSingle(String downLoadUri, String destinationUri,FileDownLoaderCallBack callBack) { FileDownloader.getImpl().create(downLoadUri).setPath(destinationUri).setListener(fileDownloadListener(callBack)).start();}// 下载方法private FileDownloadListener fileDownloadListener(final FileDownLoaderCallBack callBack) { return new FileDownloadListener() { @Override protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) { //等待,已经进入下载队列 } @Override protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) { //下载进度回调 if (callBack != null){ callBack.downLoadProgress(task,soFarBytes,totalBytes); } } @Override protected void completed(BaseDownloadTask task) { //完成整个下载过程 if (callBack != null){ callBack.downLoadCompleted(task); } } @Override protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) { //暂停下载 } @Override protected void error(BaseDownloadTask task, Throwable e) { //下载出现错误 if (callBack != null){ callBack.downLoadError(task,e); } } @Override protected void warn(BaseDownloadTask task) { //在下载队列中(正在等待/正在下载)已经存在相同下载连接与相同存储路径的任务 } };} public interface FileDownLoaderCallBack { //文件是否下载完成 void downLoadCompleted(BaseDownloadTask task); //文件是否下载失败 void downLoadError(BaseDownloadTask task, Throwable e); //文件下载进度 void downLoadProgress(BaseDownloadTask task, int soFarBytes, int totalBytes); }}
解压Zip文件
public class ZipUtils { public static final String TAG = "ZIP"; public ZipUtils() { }public static void UnZipFolder(String zipFileString, String outPathString) throws Exception { ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; String szName = ""; while ((zipEntry = inZip.getNextEntry()) != null) { szName = zipEntry.getName(); if (zipEntry.isDirectory()) { szName = szName.substring(0, szName.length() - 1); File folder = new File(outPathString + File.separator + szName); folder.mkdirs(); } else { Log.e(TAG, outPathString + File.separator + szName); File file = new File(outPathString + File.separator + szName); if (!file.exists()) { Log.e(TAG, "Create the file:" + outPathString + File.separator + szName); file.getParentFile().mkdirs(); file.createNewFile(); } // 获取文件的输出流 FileOutputStream out = new FileOutputStream(file); int len; byte[] buffer = new byte[1024]; // 读取(字节)字节到缓冲区 while ((len = inZip.read(buffer)) != -1) { // 从缓冲区(0)位置写入(字节)字节 out.write(buffer, 0, len); out.flush(); } out.close(); } } inZip.close();}public static void UnZipFolder(String zipFileString, String outPathString, String szName) throws Exception { ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; while ((zipEntry = inZip.getNextEntry()) != null) { //szName = zipEntry.getName(); if (zipEntry.isDirectory()) { //获取部件的文件夹名 szName = szName.substring(0, szName.length() - 1); File folder = new File(outPathString + File.separator + szName); folder.mkdirs(); } else { Log.e(TAG, outPathString + File.separator + szName); File file = new File(outPathString + File.separator + szName); if (!file.exists()) { Log.e(TAG, "Create the file:" + outPathString + File.separator + szName); file.getParentFile().mkdirs(); file.createNewFile(); } // 获取文件的输出流 FileOutputStream out = new FileOutputStream(file); int len; byte[] buffer = new byte[1024]; // 读取(字节)字节到缓冲区 while ((len = inZip.read(buffer)) != -1) { // 从缓冲区(0)位置写入(字节)字节 out.write(buffer, 0, len); out.flush(); } out.close(); } } inZip.close();}public static void ZipFolder(String class="lazy" data-srcFileString, String zipFileString) throws Exception { //创建ZIP ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString)); //创建文件 File file = new File(class="lazy" data-srcFileString); //压缩 // LogUtils.LOGE("---->"+file.getParent()+"==="+file.getAbsolutePath()); ZipFiles(file.getParent()+ File.separator, file.getName(), outZip); //完成和关闭 outZip.finish(); outZip.close();}private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) throws Exception { // LogUtils.LOGE("folderString:" + folderString + "\n" +"fileString:" + fileString + "\n=========================="); if (zipOutputSteam == null) return; File file = new File(folderString + fileString); if (file.isFile()) { ZipEntry zipEntry = new ZipEntry(fileString); FileInputStream inputStream = new FileInputStream(file); zipOutputSteam.putNextEntry(zipEntry); int len; byte[] buffer = new byte[4096]; while ((len = inputStream.read(buffer)) != -1) { zipOutputSteam.write(buffer, 0, len); } zipOutputSteam.closeEntry(); } else { //文件夹 String fileList[] = file.list(); //没有子文件和压缩 if (fileList.length <= 0) { ZipEntry zipEntry = new ZipEntry(fileString + File.separator); zipOutputSteam.putNextEntry(zipEntry); zipOutputSteam.closeEntry(); } //子文件和递归 for (int i = 0; i < fileList.length; i++) { ZipFiles(folderString+fileString+"/", fileList[i], zipOutputSteam); } }}public static InputStream UpZip(String zipFileString, String fileString) throws Exception { ZipFile zipFile = new ZipFile(zipFileString); ZipEntry zipEntry = zipFile.getEntry(fileString); return zipFile.getInputStream(zipEntry);}public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile) throws Exception { List<File> fileList = new ArrayList<File>(); ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; String szName = ""; while ((zipEntry = inZip.getNextEntry()) != null) { szName = zipEntry.getName(); if (zipEntry.isDirectory()) { // 获取部件的文件夹名 szName = szName.substring(0, szName.length() - 1); File folder = new File(szName); if (bContainFolder) { fileList.add(folder); } } else { File file = new File(szName); if (bContainFile) { fileList.add(file); } } } inZip.close(); return fileList;}}
下载:
File file = new File(Constants.saveH5FilePath); if (file.exists()) { file.delete(); } //开始下载ZIP压缩包 FileDownloadUtils.getInstance().startDownLoadFileSingle(bean.getUrl(), Constants.saveH5FilePath, new FileDownloadUtils.FileDownLoaderCallBack() { @Override public void downLoadCompleted(BaseDownloadTask task) { try { //解压ZIP压缩包 ZipUtils.UnZipFolder(Constants.saveH5FilePath, Constants.unH5ZipPath); PreferencesUtil.getInstance().saveParam("H5VersionName", H5VersionName); } catch (Exception e) { e.printStackTrace(); } } @Override public void downLoadError(BaseDownloadTask task, Throwable e) { } @Override public void downLoadProgress(BaseDownloadTask task, int soFarBytes, int totalBytes) { } });
webView 加载:
mWebSe.loadUrl("file:"+ Constants.unH5ZipPath+"/index.html");
感谢你能够认真阅读完这篇文章,希望小编分享的“android中H5本地缓存加载优化的方法”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341