android studio 读取assets中zip文件
2016-01-16 17:50
453 查看
今天做一个从assets获取zip压缩包中的图片,显示在recyclerview中但怎么获取都获取不到
后来想起 eclipse 与android studio 中项目的结构是不一样的,assets不能直接建在项目下
应建立在src >> main >>assets
解读feng.zip 压缩包中的图片,你可以直接保存到sd卡中,也可以直接简单保存在list集合中
1:直接保存在list集合中的一种方法
2:解读之后保存到sd中,然后你可以从sd卡直接获取图片的一种方式
后来想起 eclipse 与android studio 中项目的结构是不一样的,assets不能直接建在项目下
应建立在src >> main >>assets
解读feng.zip 压缩包中的图片,你可以直接保存到sd卡中,也可以直接简单保存在list集合中
1:直接保存在list集合中的一种方法
List<Bitmap> btms = new ArrayList<>(); public void unzip(String assestsname){ btms.clear(); try { InputStream open = getResources().getAssets().open(assestsname); ZipInputStream zipinputstream = new ZipInputStream(open); ZipEntry nextEntry = zipinputstream.getNextEntry(); while(nextEntry != null){ if(!nextEntry.isDirectory()){//判断不是文件 if(nextEntry.getName().contains(".jpg")){//我这里里是jpg格式图片所以简单操作 Bitmap bitmap = BitmapFactory.decodeStream(zipinputstream); btms.add(bitmap); } } nextEntry = zipinputstream.getNextEntry(); } Log.i("test","===什么长度=="+btms.size()); zipinputstream.close(); } catch (Exception e) { e.printStackTrace(); } }
2:解读之后保存到sd中,然后你可以从sd卡直接获取图片的一种方式
try { if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ String path = Environment.getExternalStorageDirectory().getAbsolutePath(); AssetManager assets = getResources().getAssets();
//String[] list = assets.list("header/");unZip("header/feng.zip", path); }else{ Log.i("test","没有没有sd卡"); } } catch (Exception e) { e.printStackTrace(); }
*/ public void unZip( String assetName, String savefilename) throws IOException { // 创建解压目标目录 File file = new File(savefilename); // 如果目标目录不存在,则创建 if (!file.exists()) { file.mkdirs(); } InputStream inputStream = getAssets().open(assetName); // inputStream =getClass().getResourceAsStream(assetName;//也可以进行流解读 ZipInputStream zipInputStream = new ZipInputStream(inputStream); // 读取一个进入点 ZipEntry nextEntry = zipInputStream.getNextEntry(); byte[] buffer = new byte[1024 * 1024]; int count = 0; // 如果进入点为空说明已经遍历完所有压缩包中文件和目录 while (nextEntry != null) { // 如果是一个文件夹 if (nextEntry.isDirectory()) { file = new File(savefilename + File.separator + nextEntry.getName()); if ( !file.exists()) { file.mkdir(); } } else { // 如果是文件那就保存 file = new File(savefilename + File.separator + nextEntry.getName()); // 则解压文件 if ( !file.exists()) { file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); while ((count = zipInputStream.read(buffer)) != -1) { fos.write(buffer, 0, count); } fos.close(); } } //这里很关键循环解读下一个文件 nextEntry = zipInputStream.getNextEntry(); } zipInputStream.close(); }
相关文章推荐
- 关于android MTK相机L版本,切换屏幕比例后,分辨率随之改变,但重新进入相机后原有分辨率不再生效问题
- Android学习笔记-实现toolBar跟随ListView滑动显示与隐藏
- Android JNI通过C++调用JAVA
- Android应用开发学习笔记
- android wear-Showing Information in Watch Faces and Creating InteractiveWatch Faces, Providing Confi
- 18、数据存储_01前言(这种东西最难写了)
- android 之下拉刷新
- 全局大喇叭,广播机制初篇。
- Android性能优化之Splash页应该这样设计
- Android自适应不同屏幕几种方法
- Android研发规范
- android-invalidate和postInvalidate 的区别及使用
- android之LruCache源代码解析
- Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
- android中AudioRecord使用
- android保存图片到sdcard后,在图片浏览器不能马上显示出来
- android 控件显示和隐藏时增加动画效果
- android wear-Drawing Watch Faces
- Android TV Input Framework(TIF)--1 概述
- Android 4.4 meminfo 实现分析