动态设置icon图片。服务器下载解压缩包。
最近公司提了个需求,要求动态设置Icon。什么意思呢? 因为之前过年的时候,我们的app为了迎合过年的喜气,所有首页部分icon改成了象征春节的icon,因为每次更换需重新打包发版,导致更改周期长,不方便。 于是就提了这个需求,要求接收服务器发来的一个压缩包,压缩包里包含了指定的图片,下载后设置到icon上。
根据这个需求分析呢, 首先我需要解压缩 文件。
我用的AsyncTask.
//书写的调用下载方法。
public void getFileImg(Context context) { initData(context);//控制下载路径,方便解压 DownloadTask downloadTask = new DownloadTask();//下载 downloadTask.execute(); }
//控制下载文件路径,以及文件解压路径。 判断是否已有文件夹,么有创建。
public void initData(Context context) { try { mDownloadUrl = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/";//下载文件路径 oDownloadUrl = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/main_img";//解压文件路径 mUrl = new URL("*******elm1.zip");//zip网址 mFileName = new File(mUrl.getFile()).getName(); mFile = new File(mDownloadUrl, mFileName); Log.e(TAG, "out=" + mDownloadUrl + ", name=" + mFileName + ",mUrl.getFile()=" + mUrl.getFile()); if (mFile.exists()) {//判断是否已有文件。 mFile.delete(); } else { mFile.mkdirs(); } if (mFile.exists()) Log.e(TAG, "存在"); else Log.e(TAG, "不存在"); } catch (MalformedURLException e) { e.printStackTrace(); } }
//下载文件
private class DownloadTask extends AsyncTask<Void, Integer, Long> { @Override protected Long doInBackground(Void... params) { return download(); } @Override protected void onPostExecute(Long aLong) { super.onPostExecute(aLong); ProgressHelper.hide(); ToaskUtils.showToast("下载结束:正在分解");//自己封装的toast try { zipFile = new ZipFile(mDownloadUrl + "/" + mFileName); if (null != zipFile) { ZipExtractorTask task = new ZipExtractorTask();//下载结束调用解压 task.execute(); } else { ToaskUtils.showToast("请刷新当前页面"); } } catch (IOException e) { e.printStackTrace(); } } private long download() { URLConnection connection = null; int bytesCopied = 0; try { try { connection = mUrl.openConnection(); } catch (IOException e) { e.printStackTrace(); } int length = connection.getContentLength(); if (mFile.exists() && length == mFile.length()) { Log.e(TAG, "文件" + mFile.getName() + "已存在!!"); return 0l; } mOutputStream = new FileOutputStream(mFile); bytesCopied = copy(connection.getInputStream(), mOutputStream); if (bytesCopied != length && length != -1) { Log.e(TAG, "Download incomplete bytesCopied=" + bytesCopied + ", length" + length); } Log.e(TAG, "文件长度" + mFile.length()); mOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bytesCopied; } }
文件已经下载完毕, 接下来就是 解压文件夹了。 因为公司给的是zip格式,所以文章无其他压缩格式解压方式。
调用解压:
private class ZipExtractorTask extends AsyncTask<Void, Integer, Long> { @Override protected Long doInBackground(Void... params) { return unzip();//解压 } @Override protected void onPostExecute(Long aLong) { super.onPostExecute(aLong); ToaskUtils.showToast("分解完毕"); String img_path = oDownloadUrl + "/" + oFileName;//oDownLoad为解压地址 Bitmap bmp = BitmapFactory.decodeFile(img_path); getBitmap.setBitmap(bmp);//getBitmap为自定义接口回调,可自己定义。 } }
private long unzip() {//上述调用压缩方式 long extractedSize = 0L; Enumeration<ZipEntry> entries; try { entries = (Enumeration<ZipEntry>) zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { continue; } oFileName = entry.getName(); File destination = new File(oDownloadUrl, entry.getName()); if (!destination.getParentFile().exists()) { Log.e(TAG, "make=" + destination.getParentFile().getAbsolutePath()); destination.getParentFile().mkdirs(); } if (destination.exists()) { } FileOutputStream outStream = new FileOutputStream(destination); extractedSize += copy(zipFile.getInputStream(entry), outStream); outStream.close(); } } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } return extractedSize; }
基本已经完毕了,当前只获取了一张图片,如果你需要更多, 则需要你自己进行处理,这里就不说了。
再解压缩完毕之后,转换为bitmap。其中getBitmap是我自己定义的接口,你可以自己定义。
public void setGetBitmap(getBitmap getBitmap) {//为了回调到首页。 this.getBitmap = getBitmap; } public interface getBitmap { void setBitmap(Bitmap bitmap); }
接下来就是首页的调用了:
DownloadImaUtils downloadImaUtils = new DownloadImaUtils(); downloadImaUtils.getFileImg(NewTrainMainActivity.this); downloadImaUtils.setGetBitmap(new DownloadImaUtils.getBitmap() { @Override public void setBitmap(Bitmap bitmap) { if (null != bitmap) { plane_search_image.setImageBitmap(bitmap);//设置到控件上。 } } });
到此结束,当然你有更好的方式 可以留言,大家互相探讨。
切换的效果图。
不喜勿喷,谢谢 。