从android中的字节数组加载bmp图像?

时间:2012-09-18 03:41:40

标签: android android-layout android-intent bmp

我是android的新手,我开发了一个示例应用程序,它从SD卡加载图像并使用图像视图控件显示为位图图像。 现在我想修改我的应用程序,比如从字节数组加载bmp我有原始图像,宽度和高度,是否有人为此做过示例?

3 个答案:

答案 0 :(得分:5)

使用下面的代码将字节数组转换为位图,并将此位图显示到ImageView中。

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);

并在下面看到SO链接以获取更多信息。

Convert ByteArray to Bitmap

答案 1 :(得分:1)

我认为您可以使用BitmapFactory

public static Bitmap decodeByteArray (byte[] data, int offset, int length)

有关详细信息,请look here

答案 2 :(得分:0)

如果您的图片在Drawable文件夹中,请尝试使用此代码

    Drawable drawable= getResources().getDrawable(R.drawable.yourimage);

    //Type cast to BitmapDrawable
    Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

    //Write a compressed version of the bitmap to the specified outputstream via compress method.    
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    byte[] buffer  = stream.toByteArray();