我正在实现一个应用程序,其中活动显示全屏图像视图。现在我在应用程序的文件目录中下载了一些图像。我想在我正在缩小较大图像的活动中将图像设置为图像视图。使用应用程序 10 到 15 分钟后出现 OutOfMemory 错误。错误在 BitmapFactory 的 decodeFile 中。这是我的代码:
public 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) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromFile(String imagePath,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(imagePath, options);
}
现在我给 reqHeight = 500 和 reqWidth = 500
此代码在 Android 指南页面中给出。我有什么遗漏的吗,如果有,请帮助我。我尝试了很多东西,但没有得到任何解决方案。