2

私はフラグメントを使用しており、ビューページャーがあります。ImageLoader を使用してビューページャーに画像を配置しました。このフラグメント (タブ上にある) を約 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;
    }
}
4

0 に答える 0