gpt4 book ai didi

android - 滚动 ListView 时出现内存不足异常?

转载 作者:行者123 更新时间:2023-11-29 15:20:49 24 4
gpt4 key购买 nike

我引用了很多资源,但找不到正确的答案,

我制作了一个自定义适配器以在 ListView 中查看图像。此图像是从存储卡中检索的。一切运行良好,但当我滚动浏览 ListView 时,我得到了一个 OutOfMemory 异常。我已经发布了用于从 SD 卡中检索图像的代码。

public void getFromSdcard() {

File file = new File(
android.os.Environment.getExternalStorageDirectory(),
"Tiles/.NoMedia");

if (file.isDirectory()) {
listFile = file.listFiles();

for (int i = 0; i < listFile.length; i++) {

f.add(listFile[i].getAbsolutePath());

}
}
}

这里 f 是字符串的数组列表,我将它传递给自定义适配器,下面是我的自定义适配器的代码。

public class NewImageAdapter extends ArrayAdapter<Image> {

private ArrayList<Image> objects;
String packageName;
Activity act;

public NewImageAdapter(Activity context, int image_layout,
ArrayList<Image> objects) {
super(context, image_layout, objects);
this.act = context;
this.objects = objects;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View v = convertView;

if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}

Image i = objects.get(position);

if (i != null) {
ImageView iv = (ImageView) v.findViewById(R.id.imagemenu123);
// TextView tv = (TextView) v.findViewById(R.id.commandText);
if (iv != null) {

Bitmap myBitmap = BitmapFactory.decodeFile(i.getImagePath());

iv.setImageBitmap(myBitmap);
// iv.setImageBitmap(i.getImageBitmap());
// tv.setText("Tiles Images");
}

}
return v;
}

我的问题的任何解决方案:

最佳答案

使用这个概念对你有帮助,然后在 ImageView 上设置图像位图

public static Bitmap convertBitmap(String path)   {

Bitmap bitmap=null;
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inDither=false; //Disable Dithering mode
bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024];


File file=new File(path);
FileInputStream fs=null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
if(fs!=null)
{
bitmap=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
}
} catch (IOException e) {

e.printStackTrace();
} finally{
if(fs!=null) {
try {
fs.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}

return bitmap;
}

如果你想从高度和宽度为 60 和 60 的大图像制作小图像并快速滚动 ListView ,那么使用这个概念

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
int reqHeight) {

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
return bmp;
}

public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {

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;
}

希望对你有帮助。

您可以从开发者网站寻求帮助 Here

关于android - 滚动 ListView 时出现内存不足异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18376147/

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