更改 android 中位图的大小以避免 OOM
Change the size of a bitmap in android to avoid OOM
我一直在努力更改从我的服务器下载的位图的大小,因为我知道这是我遇到的 OOM 错误的问题。我已经尝试了其他示例和这个示例 https://developer.android.com/training/displaying-bitmaps/load-bitmap.html 但我无法确定如何使用它以及在何处使用它。我想将我的位图缩小到屏幕分辨率,但无法拼接起来。感谢您对此的任何帮助。
也不是说这是在 AsyncTask
中完成的,如果这会有所作为的话。
这是我设置位图的代码:
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
BitmapFactory.Options options = new BitmapFactory.Options();
private final WeakReference<ImageView> imageViewReference;
Resources resources;
public ImageDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params)
{
return downloadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
Log.d("HTTPS No go", bitmap.toString());
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
//Scale the bitmap to a smaller size
imageView.setImageBitmap(bitmap);
} else {
Log.d("Downloading the image: ", "No Image found");
}
}
}
}
//URL connection to download the image
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try{
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
//check if the HTTP status code is equal to 200, which means that it is ok
if (statusCode != 200) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
/*
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
int sampleSize = calculateInSampleSize(options, imageWidth, imageHeight);
*/
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
}catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} {}
return null;
}
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) {
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;
}
}
更新:
这稍微修复了它。它允许我只注销一次,但是当我第二次注销时,它会因旧的 OOM 错误而崩溃。
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=1; //try to decrease decoded image
Bitmap bitmap = BitmapFactory.decodeStream(inputStream , null, options);
return bitmap;
}
}catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} {}
我使用了这个代码:
Bitmap outBitmap;
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photoPath, o);
// The new size we want to scale to ensure memory usage is optimal
int targetWidth;
int targetHeight;
if (o.outHeight > o.outWidth) {
targetWidth = getResources().getInteger(R.integer.pic_width_px);
targetHeight = getResources().getInteger(R.integer.pic_height_px);
} else if (o.outHeight == o.outWidth) {
targetWidth = targetHeight = getResources().getInteger(R.integer.pic_width_px);
} else {
targetWidth = getResources().getInteger(R.integer.pic_width_px);
targetHeight = getResources().getInteger(R.integer.pic_height_px);
}
if (o.outWidth <= targetWidth && o.outHeight <= targetHeight) {
// Return image as is without any additional scaling
Bitmap origBitmap = BitmapFactory.decodeFile(photoPath, null);
outBitmap = Bitmap.createBitmap(origBitmap, 0, 0, o.outWidth, o.outHeight, m, true);
origBitmap.recycle();
return outBitmap;
}
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= targetWidth &&
o.outHeight / scale / 2 >= targetHeight) {
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
scaleOptions.inSampleSize = scale;
Bitmap scaledBitmap = BitmapFactory.decodeFile(photoPath, scaleOptions);
return Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), m, true);
我认为您需要在将图像读入内存之前更改图像大小。
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try{
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
//check if the HTTP status code is equal to 200, which means that it is ok
if (statusCode != 200) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
//scale down the image and load
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
options.inSampleSize = calculateInSampleSize(options, 100, 100);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(inputStream, null, options); //I'm not sure here, because the inputStream used twice.
}
}catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} {}
return null;
}
感谢所有提供帮助的人。我发现我的问题不是下载位图的大小,因为每次将它们添加到我的回收视图时,我都会自动调整它们的大小。这是由我在登录页面上播放的 gif 引起的,它只占用了那么多内存,一旦任何其他东西占用了内存,它就会杀死设备。
非常感谢这里的每一个人,谢谢你们。我自己认为问题出在图像下载上,因为这是通常的罪魁祸首。
几天前我遇到了同样的问题。理论上有两种方法可以做到这一点。要么你完全下载图像然后调整图像大小,要么你必须让你的服务器而不是应用程序来做这件事。我更喜欢第二种解决方案,我在其中发送所需的宽度、高度和所需的图像。服务器计算可能的比例,然后缩小尺寸并 returns 返回,打印图像。之后我只使用 HttpURLConnection 下载位图并从我的连接的输入流创建位图没有任何问题。
你的错误呢,也许你正在尝试先从流中计算然后创建它。当然它会导致崩溃,因为你正在尝试第二次读取输入流。在读取图像的元数据以了解尺寸时,您的光标已经移动到流中。现在当它尝试创建位图时失败了,因为它不是从流的第 0 个字节开始的。但是在中间的某个地方,你的光标上次停止的地方。因此,如果需要读取流两次,则需要先将输入流复制到某处才能读取流两次。希望对你有帮助。
我一直在努力更改从我的服务器下载的位图的大小,因为我知道这是我遇到的 OOM 错误的问题。我已经尝试了其他示例和这个示例 https://developer.android.com/training/displaying-bitmaps/load-bitmap.html 但我无法确定如何使用它以及在何处使用它。我想将我的位图缩小到屏幕分辨率,但无法拼接起来。感谢您对此的任何帮助。
也不是说这是在 AsyncTask
中完成的,如果这会有所作为的话。
这是我设置位图的代码:
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
BitmapFactory.Options options = new BitmapFactory.Options();
private final WeakReference<ImageView> imageViewReference;
Resources resources;
public ImageDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params)
{
return downloadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
Log.d("HTTPS No go", bitmap.toString());
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
//Scale the bitmap to a smaller size
imageView.setImageBitmap(bitmap);
} else {
Log.d("Downloading the image: ", "No Image found");
}
}
}
}
//URL connection to download the image
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try{
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
//check if the HTTP status code is equal to 200, which means that it is ok
if (statusCode != 200) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
/*
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
int sampleSize = calculateInSampleSize(options, imageWidth, imageHeight);
*/
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
}catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} {}
return null;
}
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) {
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;
}
}
更新:
这稍微修复了它。它允许我只注销一次,但是当我第二次注销时,它会因旧的 OOM 错误而崩溃。
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=1; //try to decrease decoded image
Bitmap bitmap = BitmapFactory.decodeStream(inputStream , null, options);
return bitmap;
}
}catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} {}
我使用了这个代码:
Bitmap outBitmap;
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photoPath, o);
// The new size we want to scale to ensure memory usage is optimal
int targetWidth;
int targetHeight;
if (o.outHeight > o.outWidth) {
targetWidth = getResources().getInteger(R.integer.pic_width_px);
targetHeight = getResources().getInteger(R.integer.pic_height_px);
} else if (o.outHeight == o.outWidth) {
targetWidth = targetHeight = getResources().getInteger(R.integer.pic_width_px);
} else {
targetWidth = getResources().getInteger(R.integer.pic_width_px);
targetHeight = getResources().getInteger(R.integer.pic_height_px);
}
if (o.outWidth <= targetWidth && o.outHeight <= targetHeight) {
// Return image as is without any additional scaling
Bitmap origBitmap = BitmapFactory.decodeFile(photoPath, null);
outBitmap = Bitmap.createBitmap(origBitmap, 0, 0, o.outWidth, o.outHeight, m, true);
origBitmap.recycle();
return outBitmap;
}
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= targetWidth &&
o.outHeight / scale / 2 >= targetHeight) {
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
scaleOptions.inSampleSize = scale;
Bitmap scaledBitmap = BitmapFactory.decodeFile(photoPath, scaleOptions);
return Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), m, true);
我认为您需要在将图像读入内存之前更改图像大小。
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try{
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
//check if the HTTP status code is equal to 200, which means that it is ok
if (statusCode != 200) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
//scale down the image and load
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
options.inSampleSize = calculateInSampleSize(options, 100, 100);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(inputStream, null, options); //I'm not sure here, because the inputStream used twice.
}
}catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} {}
return null;
}
感谢所有提供帮助的人。我发现我的问题不是下载位图的大小,因为每次将它们添加到我的回收视图时,我都会自动调整它们的大小。这是由我在登录页面上播放的 gif 引起的,它只占用了那么多内存,一旦任何其他东西占用了内存,它就会杀死设备。
非常感谢这里的每一个人,谢谢你们。我自己认为问题出在图像下载上,因为这是通常的罪魁祸首。
几天前我遇到了同样的问题。理论上有两种方法可以做到这一点。要么你完全下载图像然后调整图像大小,要么你必须让你的服务器而不是应用程序来做这件事。我更喜欢第二种解决方案,我在其中发送所需的宽度、高度和所需的图像。服务器计算可能的比例,然后缩小尺寸并 returns 返回,打印图像。之后我只使用 HttpURLConnection 下载位图并从我的连接的输入流创建位图没有任何问题。
你的错误呢,也许你正在尝试先从流中计算然后创建它。当然它会导致崩溃,因为你正在尝试第二次读取输入流。在读取图像的元数据以了解尺寸时,您的光标已经移动到流中。现在当它尝试创建位图时失败了,因为它不是从流的第 0 个字节开始的。但是在中间的某个地方,你的光标上次停止的地方。因此,如果需要读取流两次,则需要先将输入流复制到某处才能读取流两次。希望对你有帮助。