gpt4 book ai didi

java - Android运行时: java. lang.OutOfMemoryError

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:18 30 4
gpt4 key购买 nike

我试图加载 ImageView 中的所有图像,但在加载 12 个图像后,应用程序因内存不足而崩溃

我尝试回收位图,但失败并无法重用位图 Canvas

这是我的代码

public void LoadImageArray() {
Bitmap mybitmap;
LinearLayout layout = (LinearLayout) findViewById(R.id.image_container);
//LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100);
String path= Environment.getExternalStorageDirectory().toString()+"/Pictures";
File f=new File(path);
File file[]=f.listFiles();
BitmapFactory.Options options=new BitmapFactory.Options();
for (int i = 0; i < 13; i++) {
layoutParams.setMargins(1, 1, 1, 1);
layoutParams.gravity = Gravity.LEFT;
ImageView imageView = new ImageView(this);

int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

String imageType=options.outMimeType;
if(imageWidth > imageHeight) {
options.inSampleSize = calculateInSampleSize(options,100,100);//if landscape
} else{
options.inSampleSize = calculateInSampleSize(options,100,100);//if portrait
}
options.inJustDecodeBounds = false;
//Bitmap
try {
File efile=new File(file[i].getAbsolutePath());
if(efile.exists()) {
mybitmap = BitmapFactory.decodeFile(file[i].getAbsolutePath(), options);


imageView.setLayoutParams(layoutParams);
imageView.setImageBitmap(mybitmap);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
imageView.setColorFilter(34);
imageView.setClickable(true);

if (mybitmap != null) {
layout.addView(imageView);
// if (!((BitmapDrawable) imageView.getDrawable()).getBitmap().isRecycled()) {
// ((BitmapDrawable) imageView.getDrawable()).getBitmap().recycle();
// }
}
//else {layout.addView(imageView);}
Log.d("Files", file[i].getName());
}
} catch(Exception e) {Log.d("TAG","Error is " +e);}
}
}

最佳答案

您没有正确使用选项,首先您创建选项,然后将 inJustDecodeBounds 设置为 true,然后对其调用解码文件,然后您按照自己的方式完成其余操作,但在您做出这样的选项后立即连续使用这些

public void LoadImageArray() {
Bitmap mybitmap;
LinearLayout layout = (LinearLayout) findViewById(R.id.image_container);
//LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100);
String path= Environment.getExternalStorageDirectory().toString()+"/Pictures";
File f=new File(path);
File file[]=f.listFiles();

for (int i = 0; i < 13; i++) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file[i].getAbsolutePath(), options);

layoutParams.setMargins(1, 1, 1, 1);
layoutParams.gravity = Gravity.LEFT;
ImageView imageView = new ImageView(this);

int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

String imageType=options.outMimeType;
if(imageWidth > imageHeight) {
options.inSampleSize = calculateInSampleSize(options,100,100);//if landscape
} else{
options.inSampleSize = calculateInSampleSize(options,100,100);//if portrait
}
options.inJustDecodeBounds = false;
//Bitmap
try {
File efile=new File(file[i].getAbsolutePath());
if(efile.exists()) {
mybitmap = BitmapFactory.decodeFile(file[i].getAbsolutePath(), options);


imageView.setLayoutParams(layoutParams);
imageView.setImageBitmap(mybitmap);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
imageView.setColorFilter(34);
imageView.setClickable(true);

if (mybitmap != null) {
layout.addView(imageView);
// if (!((BitmapDrawable) imageView.getDrawable()).getBitmap().isRecycled()) {
// ((BitmapDrawable) imageView.getDrawable()).getBitmap().recycle();
// }
}
//else {layout.addView(imageView);}
Log.d("Files", file[i].getName());
}
} catch(Exception e) {Log.d("TAG","Error is " +e);}
}
}

java android android-imageview

关于java - Android运行时: java. lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34515002/

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