Android loading images efficient (out of memory) [duplicate]

旧巷老猫 提交于 2019-12-02 07:46:31

There was an answer in SO about freeing up memory manually. I couldn't find it now so I am pasting the answer I am implementing

Define the following function

    //To free up memory taken by adapterViews and others
private void unbindDrawables(View view) {
    if (view.getBackground() != null)
        view.getBackground().setCallback(null);

    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        imageView.setImageBitmap(null);
    } else if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++)
            unbindDrawables(viewGroup.getChildAt(i));

        if (!(view instanceof AdapterView))
            viewGroup.removeAllViews();
    }
}

then in your onDestroy method use

unbindDrawables(findViewById(R.id.view_to_unbind));
System.gc();

This has stopped my app from crashing on orientation change.

You load image thumbnail in grid view not the main image after clicking on gridview you make a request to load the main image. Store thumbnails on server also.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!