gpt4 book ai didi

java - BaseAdapter 内存不足错误

转载 作者:行者123 更新时间:2023-12-02 11:52:16 25 4
gpt4 key购买 nike

我将使用下面的代码从服务器(URL)加载适配器的图像。它适用于新型移动设备。但是,旧模型会崩溃并返回“java.lang.OutOfMemoryError”。它将把 A 行或 B 行标记为错误。如何避免这个错误呢?或者还有其他图书馆可以完成我的目标吗?请帮忙,谢谢!!

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
ProgressBar progressBar;

public DownloadImageTask(ImageView bmImage, ProgressBar progressBar) {
this.bmImage = bmImage;
this.progressBar = progressBar;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {



InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in); //Line A

int resultWidth = mIcon11.getWidth();
int resultHeight = mIcon11.getHeight();
while(resultWidth > 256 && resultHeight > 256) {
resultWidth *= 0.5;
resultHeight *= 0.5;
}
mIcon11 = Bitmap.createScaledBitmap(mIcon11, (int) (resultWidth), (int) (resultHeight), true);

} catch (OutOfMemoryError e){

byte[] byteArr = new byte[0];
byte[] buffer = new byte[1024];
int len;
int count = 0;


InputStream in = null;
try {
in = new java.net.URL(urldisplay).openStream();


while ((len = in.read(buffer)) > -1) {
if (len != 0) {
if (count + len > byteArr.length) {
byte[] newbuf = new byte[(count + len) * 2]; //Line B
System.arraycopy(byteArr, 0, newbuf, 0, count);
byteArr = newbuf;
}

System.arraycopy(buffer, 0, byteArr, count, len);
count += len;
}
}


final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(byteArr, 0, count, options);

options.inSampleSize = calculateInSampleSize(options, 256, 256);
options.inPurgeable = true;
options.inInputShareable = true;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;


mIcon11 = BitmapFactory.decodeByteArray(byteArr, 0, count, options);

} catch (IOException e1) {
e1.printStackTrace();
}

} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}



return mIcon11;
}

protected void onPostExecute(Bitmap result) {

bmImage.setImageBitmap(result);

progressBar.setVisibility(View.GONE);
progressBar.setIndeterminate(false);
bmImage.setVisibility(View.VISIBLE);

}


public 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) {

// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}


return inSampleSize;
}

}

最佳答案

您可以使用GLIDE .

阅读有关 Loading Large Bitmaps 的官方指南。您应该调整大小图像。

To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.

代码结构

  • A bitmap sampled down from the original with the same aspect ratio and dimensions that are equal to or greater than the requested width and height
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;
}

关于java - BaseAdapter 内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47809423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com