1

I've developing a android application and there's a feature in it that loads some images using asynctask. I think if i can save these images as cache i can boost the performance of the app as i am loading a lot of images. How can i do this? How can i keep cache in my android application?

My class

public class MovieFragment extends Fragment {

    private ViewPager viewPager;
    private PageAdapter pageAdapter;
    private ViewPageAdapter adapter;
    private ArrayList<BaseElement> filmCategory;
    private Fragment fragment;
    private Activity activity;
    private CommonVariable commonVariable;
    private FilmCategory category;
    private Dialog dialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.movie_fragment, container, false);

        fragment = this;

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();



        viewPager = (ViewPager) view.findViewById(R.id.news_page_viewpager);

        dialog = new Dialog(this.getActivity(),
                android.R.style.Theme_Translucent_NoTitleBar);


        new BackGround().execute();

        return view;
    }

    public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            filmCategory = JSONServices.getCategory();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            commonVariable.setCategory(filmCategory);

            if (filmCategory != null) {

                pageAdapter = new PageAdapter(
                        fragment.getChildFragmentManager(), filmCategory,
                        activity);

                viewPager.setAdapter(pageAdapter);

            } else {

                Toast.makeText(activity, "No Active Internet Connection",
                        Toast.LENGTH_LONG).show();

            }

            dialog.dismiss();

            viewPager.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int position) {
                    // TODO Auto-generated method stub
                    // commonVariable.setFilmDetails((FilmCategory)
                    // category.getFilm().get(position));

                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {

                    // TODO Auto-generated method stub

                }
            });

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            dialog.setContentView(R.layout.dialog);
            dialog.show();
            super.onPreExecute();

        }

    }
CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182

2 Answers2

0

if you are downloading images from server then i think Volley library help you out. it will remove boilerplate code and easy to read it support caching also. and if you want to implement cache then follow this link. and please avoid asyctask it will not survive config changes. use asynctaskLoader :-)

mcd
  • 1,434
  • 15
  • 31
0

It seems that it might be easiest to use a third party library such as Picasso, Universal Image Loader, or Volley

I would check those out first before trying to implement your system for image caching. In most cases one of those three options will handle everything you need to accomplish.

Andrea Thacker
  • 3,440
  • 1
  • 25
  • 37