我使用片段,它有一个viewpager。我将使用 ImageLoader 的图像放到 viewpager 中。如果我调用这个片段大约 10-15 次(它在选项卡上),我会看到内存不足异常。之后,图像无法在 viewpager 中显示。
这是我的代码。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.gununurunu_fragment, container,
false);
}
shareSubject = getResources().getString(R.string.share_title);
viewpager = (ViewPager) view.findViewById(R.id.starviewpager);
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int selected) {
setPagerIndicator(viewpagerItemSize, selected);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
fillContents();
return view;
}
private void fillContents(){
lst = parser.lstProductOfDay;
ViewPagerAdapter adap = new ViewPagerAdapter(
getActivity(), lst);
viewpager.setAdapter(adap);
}
public class ViewPagerAdapter extends PagerAdapter {
private final Context context;
ArrayList<ProductOfDay> lstProductOfDay;
public ViewPagerAdapter(Context context) {
this.context = context;
}
public ViewPagerAdapter(Context context,
ArrayList<ProductOfDay> lstProductOfDay) {
this.context = context;
this.lstProductOfDay = lstProductOfDay;
}
@Override
public int getCount() {
viewpagerItemSize = lstProductOfDay.size();
return lstProductOfDay.size();
}
@Override
public Object instantiateItem(View pager, int position) {
ImageView img = new ImageView(context);
img.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ImageLoader imgLoader = new ImageLoader(getActivity());
String imageUrl = ApiAddress.getInstance().hostName
+ lstProductOfDay.get(position).getImage().getImageUri();
imgLoader.DisplayImage(imageUrl, img);
((ViewPager) pager).addView(img, 0);
return img;
}
@Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((ImageView) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
//final int REQUIRED_SIZE = 98;
//int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
//while (true) {
// if (width_tmp / 2 < REQUIRED_SIZE
// || height_tmp / 2 < REQUIRED_SIZE)
// break;
// width_tmp /= 2;
// height_tmp /= 2;
// scale *= 2;
//}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
编辑:
从 ImageLoader getBitmap 函数解决的问题
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}