记录一些常用的utils方法2
来源:互联网 发布:cm和魔趣哪个优化好点 编辑:程序博客网 时间:2024/05/16 08:20
1: image图片转成base64
/** * @param imgPath 图片地址 * @return String */ public static String imgToBase64(String imgPath) { Bitmap bitmap = null; if (imgPath !=null && imgPath.length() > 0) { bitmap = readBitmap(imgPath); } if(bitmap == null){ Log.w("图片不存在",""); } ByteArrayOutputStream out = null; try { out = new ByteArrayOutputStream(); if(bitmap != null){ bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); } out.flush(); out.close(); byte[] imgBytes = out.toByteArray(); return Base64.encodeToString(imgBytes, Base64.DEFAULT); } catch (Exception e) { // TODO Auto-generated catch block return null; } finally { try { if(out != null) { out.flush(); out.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static Bitmap readBitmap(String imgPath) { try { return BitmapFactory.decodeFile(imgPath); } catch (Exception e) { // TODO Auto-generated catch block return null; } }
2: base64转成bitmap
/** * * @param base64Data base64 * @return Bitmap */ public static Bitmap stringToBitmap(String base64Data){ //将字符串转换成Bitmap类型 Bitmap bitmap=null; try { byte[] bitmapArray; bitmapArray=Base64.decode(base64Data, Base64.DEFAULT); bitmap=BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length); } catch (Exception e) { e.printStackTrace(); } return bitmap; }
3: drawable转成bitmap
/** * * @param Drawable * @return Bitmap */ public static Bitmap drawableToBitmap( Drawable drawable){ if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof NinePatchDrawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } else { return null; } }
阅读全文
0 0
- 记录一些常用的utils方法2
- 记录一些常用的utils方法
- 记录一些常用的utils方法3
- 记录一些常用的utils方法4
- 记录一些常用的utils方法5
- 记录一些常用的utils方法6
- iOS JSONKit的一些常用方法记录
- 动画一些常用的方法记录
- YII CDbCriteria 的一些常用方法记录
- 一些常用的查找方法,记录备用
- Utils(上)一些常用的工具类
- Utils(下)一些常用的工具类
- 常用的一些记录
- 常用的一些记录
- 【造轮子】utils工具类(常用方法的封装)
- 安卓开发中常用的一些方法记录下
- 记录一些开发中常用的方法和技巧
- 记录工作中遇到的一些常用方法
- navicat 连接mysql 报1042错误 can't get hostname for your address
- play framework自定义插件plugin
- Fragment里面listview在头部嵌套gridview无限刷新错误解决
- FFmpeg源代码结构图
- SVM+HOG:训练分类器生成.xml文件
- 记录一些常用的utils方法2
- K8s 多自定义计量(custom metrics)
- C语言程序设计(34)
- 快速排序
- spark 常用函数总结
- vector
- 【Java-Web】Tomcat启动后,只能通过localhost访问,不能通过本机IP
- 查看tomcat当前连接数
- 不小心把本地的maven仓库干掉了怎么办?