簡體   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