【发布时间】: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