Android中Bitmap, Drawable, Byte之间的转化

1.  Bitmap 转化为 byte
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); //100%保存
byte[] array= out.toByteArray();

2. byte转化为bitmap
final ContentResolver contentResolver = context.getContentResolver();
final PackageManager manager = context.getPackageManager();
final Cursor c = contentResolver.query(uri, null, null, null, null);
final int icon3DIndex = c.getColumnIndexOrThrow(ColumnName);
byte[] data = c.getBlob(icon3DIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

3. bitmap转化为Drawable
Drawable drawable = new FastBitmapDrawable(bitmap);
...

4. Drawable转化为bitmap
a. BitmapDrawable, FastBitmapDrawable直接用getBitmap
b. 其他类型的Drawable用Canvas画到一个bitmap上
      Canvas canvas = new Canvas(bitmap)
      drawable.draw(canvas);


图片的获取

一、概念区别

Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB8888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。

Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。

Canvas - 名为画布,我们可以看作是一种处理过程,使用各种方法来管理Bitmap、GL或者Path路径,同时它可以配合Matrix矩阵类给图像做旋转、缩放等操作,同时Canvas类还提供了裁剪、选取等操作。

Paint - 我们可以把它看做一个画图工具,比如画笔、画刷。他管理了每个画图工具的字体、颜色、样式。

二、Android读取不同位置(drawable,asset,SDCard)的图片资源

方式一:

已将图片保存到drawable目录下,通过图片id获得Drawable或者Bitmap,此方式最常用。(若只知道图片的名称,还可以通过图片的名称获得图片的id)

(1)通过图片id获得Drawable

Drawable drawable=getResource().getDrawable(R.drawable.xxx);

(2)通过图片id获得Bitmap

Resource res=gerResource();

Bitmap bitmap=BitmapFactory.decodeResource(res, id);

(3)通过图片的名称获得图片的id(两种方法)

int id =res.getIdentifier(name, defType, defPackage); //name:图片的名,defType:资源类型(drawable,string。。。),defPackage:工程的包名

Drawable drawable=getResource().getDrawable(id);

方式二:

已将图片保存到assest目录下,知道图片的名称,通过inputstream获得图片Drawabl

或者 Bitmap

AssetManager asm=getAssetMg();

InputStream is=asm.open(name);//name:图片的名称

(1)获得Drawable

Drawable da = Drawable.createFromStream(is, null);

(2)获得Bitmap

Bitmap bitmap=BitmapFactory.decodeStream(is);

方式三: 图片保存在sdcard,通过图片的路径h

/图片路径

String imgFilePath = Environment.getExternalStorageDirectory().toString()

+ “/DCIM/device.png”;

(1)文件输入流

fis = new FileInputStream(new File(imgFilePath));//文件输入流

Bitmap bmp = BitmapFactory.decodeStream(fis);

(2)

ImageView iv = (ImageView) findViewById(R.id.image);

Bitmap bit = BitmapFactory.decodeFile("/sdcard/android.bmp");

iv.setImageBitmap(bit);

iv.setImageDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "camera.jpg").getAbsolutePath()));


三、DrawableBitmapbyte[]之间的转换

1)      DrawbleBitmap

public static Bitmap drawableToBitmap(Drawable drawable) {

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);

//canvas.setBitmap(bitmap);

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

drawable.draw(canvas);

return bitmap;

}

2) 从资源中获取Bitmap

Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

3) Bitmap byte[]

private byte[] Bitmap2Bytes(Bitmap bm){

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray();

}

4) byte[] Bitmap

private Bitmap Bytes2Bimap(byte[] b){

if(b.length!=0){

return BitmapFactory.decodeByteArray(b, 0, b.length);

}

else {

return null;

}

}

//assets文件中资源取出,并将图片从bitmap转换成drawable格式

public static Drawable getDrawableFromAssetFile(Context context,String fileName){

Bitmap image = null;

BitmapDrawable drawable=null;

try{

AssetManager am = context.getAssets();

InputStream is = am.open(fileName);

image = BitmapFactory.decodeStream(is);

drawable= new BitmapDrawable(context.getResources(), image);

is.close();

}catch(Exception e){

}

return drawable;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值