图片压缩,Base64编码后上传服务器
2015-07-16 10:55
681 查看
/* * 将图片编码成Base64字符串 */ public static String getPhotoBase64(PicRec pic) throws Exception { Bitmap bit = getBitmap(pic); String result = ""; ByteArrayOutputStream bos = null; if (bit != null) { bos = new ByteArrayOutputStream(); bit.compress(Bitmap.CompressFormat.JPEG, 90, bos); byte[] bitmapByte = bos.toByteArray(); try { if (bos != null) { bos.flush(); bos.close(); } } catch (IOException e) { e.printStackTrace(); } result = Base64.encodeToString(bitmapByte, Base64.DEFAULT); } return result; } /* *获取bitmap对象 */ public static Bitmap getBitmap(PicRec pic) throws Exception { BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPurgeable = true; options.inJustDecodeBounds = false; options.inInputShareable = true; options.inSampleSize = 5; options.inTempStorage = new byte[12 * 1024]; File file; FileInputStream fis = null; Bitmap bitmap = null; try { file = new File(pic.getUrl()); fis = new FileInputStream(file); bitmap = BitmapFactory.decodeStream(fis, null, options); } catch (OutOfMemoryError e) { throw e; } catch (Exception e) { throw e; } finally { if (fis != null) try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } return bitmap; }
相关文章推荐
- String to Integer
- autocad绘图常用知识
- 《Spring技术内幕》学习笔记8——创建AOP代理对象并对目标对象切面拦截
- 《Spring技术内幕》学习笔记9——AOP通知以及编程式AOP
- fzu-1753 Another Easy Problem-高速求N!多少个月p
- Lua for学习
- spring @Cacheable的用法
- “VMware Tools installation cannot be started manually while Easy Install is in prog
- SQl CASE WHEN 语句的嵌套使用方式
- 如何在Ubuntu 14.04中安装最新版Eclipse
- SumMenuDemo(子菜单)
- C/C++申请空间与释放空间的两种方法
- 《Spring技术内幕》学习笔记7——AOP基础
- 跨服务器查询sql语句样例
- 12款最佳Linux命令行终端工具
- AES加密和解密(使用openssl编程)
- 关于 Java Collections API 您不知道的 5 件事
- MySQL错误回显中出现乱码 “Error”显示为“Erreur”
- .NET微信公众号开发之准备工作
- 在这里写文章居然需要审核??