Android用Intent传递图片

1.直接在bundle里面传递drawable图片

这个我就不说了,有局限性,只能传递drawable,因为drawable实现了parcelable,bitmap类型的不行。

2.图片转成byte数组传递

主要说一下这个方法:

如果直接是资源bitmap,可以用下面的方法:

private byte[] Bitmap2Bytes(Bitmap bm){   
    ByteArrayOutputStream baos = new ByteArrayOutputStream();     
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);     
    return baos.toByteArray();   
   }  

如果是drawable,可以先转成bitmap,下面是代码:

public static byte[] drawable2Bytes(Drawable drawable) {


		if (drawable == null) {
			return null;
		}
		Bitmap bitmap = drawableToBitmap(drawable);


		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}
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;
	}

byte数组转化为bitmap方法:

byte buff[]=mIntent.getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);




你可能感兴趣的:(Android基础)