我正在尝试加载图像视图中的所有图像但是在12张图像之后应用程序因内存不足而崩溃
我尝试回收位图,但由于无法重复使用位图画布而失败
这是我的代码
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);}
}
}
答案 0 :(得分:0)
你没有正确使用选项首先你做出选项然后将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
答案 1 :(得分:0)
答案 2 :(得分:0)
添加bitmap.recycle();试试这个:
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);
mybitmap.recycle();// added
// 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);}
}
}