简体   繁体   中英

Byte array to Bitmap not displayed Java Android

i try to display a received byte[] as an Bitmap.

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

I don't know how big the byte Array should be and i don't know how to get the data of the inputStream in the byte[]. Can anyone help me?

Create ByteArrayOutputStream - it's an output stream in which the data is written into a byte array. Then use copy() method (taken from the Commons IO ) to copy input stream to the output stream:

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

Length of the data array will be the same as length of the data read from the input stream.

Please find copy() method below:

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

Use inputStreamToByteArray method to parse InputStream to 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();
}

Also while loading large bitmaps consider Google's suggestion. Resize them for your needs. By doing this you'll prevent many memory failures:

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

Edit: Read Google's loading large bitmaps doc. See link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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