【问题标题】:resize contact image from BufferedInputStream从 BufferedInputStream 调整联系人图像的大小
【发布时间】:2016-08-19 18:41:05
【问题描述】:

我正在尝试检查获取的联系人图像的大小并在需要时调整大小。我正在使用官方开发者 android 站点模式https://developer.android.com/training/displaying-bitmaps/load-bitmap.html 建议的,并进行了一些小改动。这是我的代码

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

public static Bitmap decodeSampledBitmapFromBufferedStream(BufferedInputStream bufferedInputStream,
                                                     int reqWidth, int reqHeight) throws IOException
{
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    // Calculate inSampleSize
    BitmapFactory.decodeStream(bufferedInputStream, new Rect(), options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(bufferedInputStream, new Rect(), options);
}

我正在从游标内部调用 decodeSampledBitmapFromBufferedStream 方法(我真的不知道这是否重要)。我的问题是选项对象的 outHeight 和 outwidth 始终为 0 并且返回位图为空。我认为这与 bufferedInoutStream 对象的重新使用有关,但我不知道如何解决它。提前致谢。

【问题讨论】:

    标签: android bitmap inputstream


    【解决方案1】:

    流一旦位置移动,您通常无法返回(除非它有倒带方法,如文件流)。您可以关闭流,然后打开一个新流,或者第一次将完整的流读入字节数组,然后两次都使用decodeByteArray

    【讨论】:

      猜你喜欢
      • 2013-07-23
      • 1970-01-01
      • 2018-01-10
      • 2012-02-24
      • 2023-03-28
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多