【发布时间】:2011-10-26 10:23:55
【问题描述】:
我有一个图像密集型的 Android 应用程序。我目前正在使用Bitmap.createScaledBitmap() 将图像缩放到所需的大小。但是,这种方法要求我在内存中已经有原始位图,它可以相当大。
如何在不先将整个内容写入本地内存或文件系统的情况下缩放正在下载的位图?
【问题讨论】:
标签: android bitmap streaming scale
我有一个图像密集型的 Android 应用程序。我目前正在使用Bitmap.createScaledBitmap() 将图像缩放到所需的大小。但是,这种方法要求我在内存中已经有原始位图,它可以相当大。
如何在不先将整个内容写入本地内存或文件系统的情况下缩放正在下载的位图?
【问题讨论】:
标签: android bitmap streaming scale
此方法将从图像中读取标题信息以确定其大小,然后读取图像并将其缩放到所需的大小,而无需为完整的原始大小的图像分配内存。
它还使用BitmapFactory.Options.inPurgeable,这似乎是一个文档稀少但理想的选项,可以在使用大量位图时防止 OoM 异常。 更新:不再使用 inPurgeable,请参阅来自 Romain 的 this note
它的工作原理是使用 BufferedInputStream 在通过 InputStream 读取整个图像之前读取图像的标头信息。
/**
* Read the image from the stream and create a bitmap scaled to the desired
* size. Resulting bitmap will be at least as large as the
* desired minimum specified dimensions and will keep the image proportions
* correct during scaling.
*/
protected Bitmap createScaledBitmapFromStream( InputStream s, int minimumDesiredBitmapWith, int minimumDesiredBitmapHeight ) {
final BufferedInputStream is = new BufferedInputStream(s, 32*1024);
try {
final Options decodeBitmapOptions = new Options();
// For further memory savings, you may want to consider using this option
// decodeBitmapOptions.inPreferredConfig = Config.RGB_565; // Uses 2-bytes instead of default 4 per pixel
if( minimumDesiredBitmapWidth >0 && minimumDesiredBitmapHeight >0 ) {
final Options decodeBoundsOptions = new Options();
decodeBoundsOptions.inJustDecodeBounds = true;
is.mark(32*1024); // 32k is probably overkill, but 8k is insufficient for some jpgs
BitmapFactory.decodeStream(is,null,decodeBoundsOptions);
is.reset();
final int originalWidth = decodeBoundsOptions.outWidth;
final int originalHeight = decodeBoundsOptions.outHeight;
// inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
decodeBitmapOptions.inSampleSize= Math.max(1,Math.min(originalWidth / minimumDesiredBitmapWidth, originalHeight / minimumDesiredBitmapHeight));
}
return BitmapFactory.decodeStream(is,null,decodeBitmapOptions);
} catch( IOException e ) {
throw new RuntimeException(e); // this shouldn't happen
} finally {
try {
is.close();
} catch( IOException ignored ) {}
}
}
【讨论】:
is.reset();上得到java.io.IOException: Mark has been invalidated
这是我的版本,基于@emmby 解决方案(谢谢!) 我已经包含了第二阶段,您可以在其中获取缩小的位图并再次对其进行缩放以完全匹配您想要的尺寸。 我的版本采用文件路径而不是流。
protected Bitmap createScaledBitmap(String filePath, int desiredBitmapWith, int desiredBitmapHeight) throws IOException, FileNotFoundException {
BufferedInputStream imageFileStream = new BufferedInputStream(new FileInputStream(filePath));
try {
// Phase 1: Get a reduced size image. In this part we will do a rough scale down
int sampleSize = 1;
if (desiredBitmapWith > 0 && desiredBitmapHeight > 0) {
final BitmapFactory.Options decodeBoundsOptions = new BitmapFactory.Options();
decodeBoundsOptions.inJustDecodeBounds = true;
imageFileStream.mark(64 * 1024);
BitmapFactory.decodeStream(imageFileStream, null, decodeBoundsOptions);
imageFileStream.reset();
final int originalWidth = decodeBoundsOptions.outWidth;
final int originalHeight = decodeBoundsOptions.outHeight;
// inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
sampleSize = Math.max(1, Math.max(originalWidth / desiredBitmapWith, originalHeight / desiredBitmapHeight));
}
BitmapFactory.Options decodeBitmapOptions = new BitmapFactory.Options();
decodeBitmapOptions.inSampleSize = sampleSize;
decodeBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565; // Uses 2-bytes instead of default 4 per pixel
// Get the roughly scaled-down image
Bitmap bmp = BitmapFactory.decodeStream(imageFileStream, null, decodeBitmapOptions);
// Phase 2: Get an exact-size image - no dimension will exceed the desired value
float ratio = Math.min((float)desiredBitmapWith/ (float)bmp.getWidth(), (float)desiredBitmapHeight/ (float)bmp.getHeight());
int w =(int) ((float)bmp.getWidth() * ratio);
int h =(int) ((float)bmp.getHeight() * ratio);
return Bitmap.createScaledBitmap(bmp, w,h, true);
} catch (IOException e) {
throw e;
} finally {
try {
imageFileStream.close();
} catch (IOException ignored) {
}
}
}
【讨论】: