【问题标题】:Byte array to Bitmap not displayed Java Android字节数组到位图不显示Java Android
【发布时间】:2014-02-24 16:02:21
【问题描述】:

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

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

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

【问题讨论】:

  • 将 InputStream 解析为 byte[] 的方法有很多。在我的回答中,我写了其中一个。在加载大型位图时,Alos 会考虑 Google 的建议。否则,可能会导致内存不足错误。

标签: java android sockets bitmap bytearray


【解决方案1】:

使用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 的加载大型位图文档。 See link.

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • (然后我有它的长度,但是如何处理它呢?创建一个新数组并执行我在顶部写的操作?)编辑:通过副本,所有数据都进入输出,然后在第三行将它们推入 Byte[] 数据,然后我可以使用 BitmapFactory 对其进行解码?
    • Edit2:我在 Android 上无法使用复制方法
    • copy() 方法的定义是答案,你必须把它粘贴到你的班级。
    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多