Android图片二进制与Bitmap、Drawable之间的转换

Java代码  
public byte[]
getBitmapByte(Bitmap bitmap){  
   ByteArrayOutputStream out
= new ByteArrayOutputStream();  
 
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);  
 
 try {  
       out.flush();  
 
     out.close();  
   } catch (IOException e)
{  
       e.printStackTrace();  
 
 }  
   return out.toByteArray();  
}

public Bitmap getBitmapFromByte(byte[] temp){  
 
 if(temp != null){  
       Bitmap bitmap =
BitmapFactory.decodeByteArray(temp, 0, temp.length);  
   
   return bitmap;  
   }else{  
 
     return null;  
   }  
}

public byte[] getBitmapByte(Bitmap
bitmap){
ByteArrayOutputStream out = new
ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
out);
try {
out.flush();
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
return out.toByteArray();
}

public
Bitmap getBitmapFromByte(byte[] temp){
if(temp != null){
Bitmap bitmap =
BitmapFactory.decodeByteArray(temp, 0, temp.length);
return
bitmap;
}else{
return null;
}
}

Java代码  
public
static Bitmap drawableToBitmap(Drawable drawable){    
 
         int width = drawable.getIntrinsicWidth();
   
           int height =
drawable.getIntrinsicHeight();    
       
   Bitmap bitmap = Bitmap.createBitmap(width, height,    

                 
 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
   
                 
         : Bitmap.Config.RGB_565);    

           Canvas canvas = new Canvas(bitmap);
   
         
 drawable.setBounds(0,0,width,height);    
   
       drawable.draw(canvas);    
 
         return bitmap;    
   
   }

public static Bitmap
drawableToBitmap(Drawable drawable){  
       
   int width = drawable.getIntrinsicWidth();  
   
       int height = drawable.getIntrinsicHeight();
 
           Bitmap bitmap =
Bitmap.createBitmap(width, height,  
         
         drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888  
           
               : Bitmap.Config.RGB_565);
 
           Canvas canvas = new
Canvas(bitmap);  
         
 drawable.setBounds(0,0,width,height);  
     
     drawable.draw(canvas);  
       
   return bitmap;  
       }

Java代码  
Drawable drawable = new FastBitmapDrawable(bitmap);

Android图片二进制与Bitmap、Drawable之间的转换的更多相关文章

  1. Android图片缓存之Bitmap详解

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...

  2. 【转】Android中dip(dp)与px之间单位转换

    Android中dip(dp)与px之间单位转换 dp这个单位可能对web开发的人比较陌生,因为一般都是使用px(像素)但是,现在在开始android应用和游戏后,基本上都转换成用dp作用为单位了,因 ...

  3. Android图片缓存之Bitmap详解(一)

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. Bitmap: Bitmap是Android ...

  4. Android 图片文件和Bitmap之间的转换

    String filePath="c:/01.jpg"; Bitmap bitmap=BitmapFactory.decodeFile(filePath); 如果图片过大,可能导致 ...

  5. Android笔记之dp与px之间的转换以及LayoutParams

    dp与px之间的转换公式 px = dp * (dpi / 160) dp = px / (dpi / 160) 其中dpi的获取方式如下 private void getDpi() { Displa ...

  6. byte、二进制、十进制数值之间的转换

    项目中遇到将字节数据文件解析成可展示的十进制,经过调查和测试得出下面的转换方法 1.将byte值转换为二进制字符串: byte byteValue = -1; // 将byte转换为8位二进制字符串 ...

  7. Android图片缓存之Lru算法

    前言: 上篇我们总结了Bitmap的处理,同时对比了各种处理的效率以及对内存占用大小.我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发 ...

  8. Android图片缓存之Glide进阶

    前言: 前面学习了Glide的简单使用(Android图片缓存之初识Glide),今天来学习一下Glide稍微复杂一点的使用. 图片缓存相关博客地址: Android图片缓存之Bitmap详解 And ...

  9. Android图片缓存之初识Glide

    前言: 前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架.技术本身就要不断的更迭,从最初的自己使用SoftReference实现自己的图片缓存,到后来做电商项目自己的实 ...

随机推荐

  1. What-are-P-NP-NP-complete-and-NP-hard

    https://www.amazon.com/Computational-Complexity-Approach-Sanjeev-Arora/dp/0521424267 http://theory.c ...

  2. python(39):argparse的用法,从外部传入指定参数

    直接上例子: # /usr/bin/env python # coding=utf8 import os import argparse import logging import sys FORMA ...

  3. Android调用Webservice发送文件

    一服务器端C#这里有三个上传方法1.uploadFile( byte []bs, String fileName); PC机操作是没有问题2. uploadImage(String filename, ...

  4. SpringMVC中的 JSR 303 数据校验框架说明

    JSR 303 是java为Bean数据合法性校验提供的标准框架,它已经包含在JavaEE 6.0中. JSR 303 通过在Bean属性上标注类似于@NotNull.@Max等标准的注解指定校验规则 ...

  5. Mac下终端(terminal)的一些快捷键

    Mac下终端(terminal)的一些快捷键 行首ctrl + a 行尾ctrl + e 两个终端窗口切换alt + 方向键 命令中,上一个单词esc + b (iterm2) 下一个单词esc + ...

  6. android adb 读写模式 挂载文件系统

    http://www.qylk.blog.163.com/blog/static/1346873562013092154430/ http://blog.sina.com.cn/s/blog_9906 ...

  7. 这到底是什么bug?---已结贴

    问题描述:全局变量,会被莫名其妙更改!打印为50,后面做比较的时候这个值为0了. 第一,我肯定没有犯低级错误,没有其他的更改,搜索全部代码,没有发现这个变量因为我程序问题导致不符合预期,同时找了两位同 ...

  8. django 事务错误 -- Transaction managed block ended with pending COMMIT/ROLLBACK

    Request Method: GET Request URL: http://192.168.128.111:8000/×××/××××/ Django Version: 1.4.8 Excepti ...

  9. axublog 1.05代码审计

    00x1 安装漏洞 install/cmsconfig.php function step4(){ $root=$_POST["root"]; $dbuser=$_POST[&qu ...

  10. 一个JS引发的血案

    转载一篇大师傅的文章: 原文链接:http://xn--i2r.ml/index.php/2017/08/05/39.html 又到了周末,闲来无聊,挖挖补天 找了个目标,发现一个站 查看源码发现一个 ...