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. [IOS]开源库RegexKitLite正则表达式的使用

    1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中. 2.工程中添加libicucore.dylib frameworks. 友情提醒:一般 ...

  2. CentOS编译安装vim

    #删除系统自带的vim rpm -qa | grep vim rpm -e vim-X11-7.4.629-5.el6_8.1.x86_64 vim-filesystem-7.4.629-5.el6_ ...

  3. 【Unity】10.1 类人动画的导入和设置

    分类:Unity.C#.VS2015 创建日期:2016-05-02 一.制作或购买类人模型 为了充分使用 Mecanim 类人动画系统和类人动画的动画重定位功能,需要先使用其他3D建模软件(例如3d ...

  4. android---笔记 AppContext extends Application

    package com.fuda; import org.apache.http.client.CookieStore; import com.fuda.model.StudentInfoModel; ...

  5. MySQL 性能跟踪方法

    https://www.cnblogs.com/zhoujinyi/p/5236705.html https://dev.mysql.com/doc/refman/5.7/en/slow-query- ...

  6. GGGGCCCC

    Evaluating and improving remembered sets in the HotSpot G1 garbage collector http://www.diva-portal. ...

  7. query compiler

    https://db.in.tum.de/teaching/ws1415/queryopt/chapter3.pdf?lang=de pi3.informatik.uni-mannheim.de/~m ...

  8. Java 用栈实现队列

    栈是先入后出,队列是先入先出.根据这个思想,可以用一个栈作为入队,另一个栈作为出队.只要把第一个栈的栈顶的元素压入第二个栈就好了,出队的时候输出第二个栈的栈顶,如果第二个栈的空了就需要不断操作从第一个 ...

  9. 数据库的ACID

    一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例进行分析. [sql] ...

  10. [Windows Azure] Getting Started with Windows Azure SQL Data Sync

    Getting Started with Windows Azure SQL Data Sync In this tutorial, you learn the fundamentals of Win ...