Fragment,ImageLoader,Viewpager以及最后的内存不足异常

时间:2012-12-19 22:24:55

标签: android android-viewpager fragment out-of-memory

我使用片段,它有一个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;
    }
}

0 个答案:

没有答案