从 BufferedInputStream 调整联系人图像的大小
resize contact image from BufferedInputStream
我正在尝试检查获取的联系人图像的大小,并在需要时调整大小。我正在使用官方开发人员 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,并且 return 位图为空。我认为这与bufferedInoutStream对象的重新使用有关,但我不知道如何解决。提前致谢。
流的位置一旦移动,通常就无法返回(除非它具有倒带方法,如文件流)。您可以关闭流,然后打开一个新流,或者第一次将完整的流读入字节数组,然后两次都使用 decodeByteArray
。
我正在尝试检查获取的联系人图像的大小,并在需要时调整大小。我正在使用官方开发人员 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,并且 return 位图为空。我认为这与bufferedInoutStream对象的重新使用有关,但我不知道如何解决。提前致谢。
流的位置一旦移动,通常就无法返回(除非它具有倒带方法,如文件流)。您可以关闭流,然后打开一个新流,或者第一次将完整的流读入字节数组,然后两次都使用 decodeByteArray
。