android 网络加载图片并保存本地,压缩,jpeg png格式分析
来源:互联网 发布:烟台网亿网络好不好 编辑:程序博客网 时间:2024/06/05 12:49
标题起的有点长,主要是不知道该怎么表达,android的图片获取和保存相信很多人都做过.有时候我们在对图片保存时会将图片压缩一下,也就是这个压缩, 会造成很多问题.通常情况下,网络获取图片均为jpeg格式,而有些时候app为了达到某些效果,会给出png的透明边图片,可是我们在压缩的时候依然用jpeg的 格式进行的压缩.这就是问题的根源.但是我们也不可能再写一套保存逻辑用png的格式,再说了,我们也不知道哪个图是png格式,哪个图是jpeg格式,对不对? 以前做项目的时候遇到过,解决了.也就没再关心过这个东西.今天突然想到一些问题,就在网上搜索有没有更好的解决方式,可是一个都没搜到,连解决这种保存方式的 帖子都没有,这页促使我把我以前的解决方式分享给大家.有更好的方式大家可以互相讨论.具体请看代码:
if(is!=null){//is就是网上获取的图片流 // 配置bitmap,防止内存溢出 BitmapFactory.Options options = new BitmapFactory.Options(); options .inPreferredConfig = Bitmap.Config.RGB_565; options .inPurgeable = true; options .inInputShareable = true;//options.inJustDecodeBounds = false;这句必须为false 也可以不写,默认是false,如果改成true返回的bitmap是空,主要是返回图片的一些尺寸信息.所以请写成false,这样返回的就是bitmap.如果有疑问可以百度一下,很多帖子写的很清楚. bitmap= BitmapFactory.decodeStream(is, null , options ); is.close(); UtilFile.saveImgToCompletePath(bitmap, path, UtilImage.getImgFormat(options)); }//UtilImage类//这个方法就是动态的根据图片类型选择压缩的图片格式,主要根据options的信息来判断图片的后缀,这样就得到了图片的类型public static CompressFormat getImgFormat(BitmapFactory.Options options){ String type = options.outMimeType; if(type!=null&&type.indexOf("png")>-1) return CompressFormat.PNG; else return CompressFormat.JPEG; }//UtilFIle类/** * 保存图片到sd卡 * @param bitmap * @param completePath : 完整路径 * @param compressFormat */public static void saveImgToCompletePath(Bitmap bitmap,String completePath,CompressFormat format) { File file = new File(completePath); File parentFile = file.getParentFile(); if (!parentFile.exists()) parentFile.mkdirs(); if (bitmap != null) { try{ FileOutputStream fos = new FileOutputStream(file); bitmap.compress(format, 100, fos); }catch(Exception e){ InputStream is = UtilImage.bitmapToInputStream(bitmap, format,0); saveFileToCompletePath(getSDDir() + file.getAbsolutePath(), is, false); } } }//UtilFile 类/** * 在SD卡上存文件 * @param completePath : 完整路径 * @param is * @return 成功file,失败null */ public static File saveFileToCompletePath(String completePath, InputStream is, boolean append) { File file = new File(completePath); File parentFile = file.getParentFile(); if (!parentFile.exists()) parentFile.mkdirs(); if (is != null) { FileOutputStream fos = null; try { fos = new FileOutputStream(file, append); byte[] b = new byte[1024]; int len = -1; while ((len = is.read(b)) != -1) { fos.write(b, 0, len); } fos.close(); is.close(); return file; } catch (Exception e) { e.printStackTrace();// LogManager.reportError("写sd文件异常",e); return null; } } else return null; }
0 0
- android 网络加载图片并保存本地,压缩,jpeg png格式分析
- Android ZoomImageView手势缩放拖动图片,并将Glide加载的网络图片保存到本地。
- 使用AsyncTask加载网络图片并保存到手机本地
- 加载JPG / JPEG / GIF / PNG 格式的图片
- Android 获取图片资源格式(png、jpeg、gif、bmp、...)
- Android下载网络图片并保存在本地相册
- ios 关于PNG和JPEG格式压缩
- android 获取http网络图片保存png
- 图片质量压缩 与 图片格式 JPEG PNG
- Android 异步加载网络图片并缓存到本地
- Android 异步加载网络图片并缓存到本地
- Android 异步加载网络图片并缓存到本地
- Android 异步加载网络图片并缓存到本地
- Android 加载网络图片并下载至本地SdCard
- C#下怎样处理并保存单色格式PNG图片?
- 获取网络图片,并保存到本地
- Android笔记-->本地图片压缩保存
- Android开发 压缩保存图片至本地
- poj 2451
- LA 11107 Life Forms
- 3D建模与处理软件简介
- Android dialog 通过背景bitmap的像素宽度进行定位显示
- 关于真机测试时INSTALL_FAILED_USER_RESTRICTED安装错误的问题
- android 网络加载图片并保存本地,压缩,jpeg png格式分析
- 数据结构学习笔记(九)跳表、字典的跳表描述
- shell脚本
- VB.Net语言 复制、删除文件
- Bit Manipulation - C语言中二进制操作
- C,C++ 宏中#与##的讲解
- extern用法 C和C++混编
- 7月小结
- 块-block(语法部分)