繁体   English   中英

位图的字节数组未显示Java Android

[英]Byte array to Bitmap not displayed Java Android

我尝试将接收到的byte []显示为位图。

InputStream stream = mySocket.getInputStream();
byte[] datas = new byte[SIZE?];
Bitmap bmp = BitmapFactory.decodeByteArray(datas, 0, datas.length);
ImageView view = new ImageView(MyServer.this);
view.setImageBitmap(bmp);
lChat.addView(view);

我不知道字节数组应该有多大,我也不知道如何在byte []中获取inputStream的数据。 谁能帮我?

创建ByteArrayOutputStream这是一个输出流,其中数据被写入字节数组。 然后使用copy()方法(从Commons IO中获取 )将输入流复制到输出流:

InputStream stream = mySocket.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(stream, output);
byte[] data = output.toByteArray();
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
...

data数组的长度将与从输入流读取的数据的长度相同。

请在下面找到copy()方法:

public static int copy(InputStream input, OutputStream output) throws IOException {
    int n, count = 0;
    byte[] buffer = new byte[4 * 1024];
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

使用inputStreamToByteArray方法将InputStream解析为byte[]

public byte[] inputStreamToByteArray(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    while ((len = inputStream.read(buffer)) > -1) {
        baos.write(buffer, 0, len);
    }
    baos.flush();

    return baos.toByteArray();
}

另外,在加载大位图时,请考虑Google的建议。 根据您的需要调整它们的大小。 通过这样做,您可以防止许多内存故障:

private Bitmap getBitmap(InputStream is, int requestedMaxWidth, int requestedMaxHeight) throws IOException {
    // parse inputStream to byte[]
    byte[] dataToBeDecode = inputStreamToByteArray(is);
    BitmapFactory.Options options = new BitmapFactory.Options();

    // First see the width and height of incoming bitmap            
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(dataToBeDecode, 0, dataToBeDecode.length, options);

    // Calculate inSampleSize for resizing
    options.inSampleSize = calculateInSampleSize(options, requestedMaxWidth, requestedMaxHeight);

    // Now resize and get the bitmap
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeByteArray(dataToBeDecode, 0, dataToBeDecode.length, options);

    return bitmap;
}

public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2
        // and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

编辑:阅读Google的加载大位图文档。 见链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM