fresco icon indicating copy to clipboard operation
fresco copied to clipboard

Always cannot show the images when loading lots of bitmaps on screen

Open KrystalJake1989 opened this issue 11 years ago • 28 comments

I use girdview to load around 30 bitmaps from sdcard on screen each time.But it only display 11 bitmaps on screen. device-2015-05-12-193017

KrystalJake1989 avatar May 12 '15 11:05 KrystalJake1989

It's same with I use, how improve it.

sangmingming avatar May 12 '15 11:05 sangmingming

I have no idea,please committer give some ideas or method to solve this problem

KrystalJake1989 avatar May 12 '15 11:05 KrystalJake1989

I find this constant MAX_SIMULTANEOUS_FILE_FETCH_AND_RESIZE in ProducerSequenceFactory

KrystalJake1989 avatar May 12 '15 12:05 KrystalJake1989

Can you give us a code snippet please? (that constant is not the problem)

IanChilds avatar May 12 '15 13:05 IanChilds

package com.koudai.weidian.buyer.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import com.facebook.drawee.view.SimpleDraweeView;
import com.koudai.weidian.buyer.R;
import com.koudai.weidian.buyer.activity.GalleryActivity;
import com.koudai.weidian.buyer.dialog.ProcessDialog;
import com.koudai.weidian.buyer.image.loader.FrescoImageLoader;
import com.koudai.weidian.buyer.util.AppUtil;

/**
 * Created by zhaoyu on 14/12/1.
 */
public class GalleryFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<Cursor>, View.OnClickListener {

    private static final int THUMBNAIL_LOAD = 1101;

    private int cellWidthAndHeight = 0;

    private GridView mGallery;
    private ImageButton mBack;
    private CursorAdapter mAdapter;
    private ProcessDialog mLoadingDialog;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.wdb_gallery_layout, container, false);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mGallery = (GridView) view.findViewById(R.id.wdb_gallery);
        mBack = (ImageButton) view.findViewById(R.id.wdb_back);
        mAdapter = new GalleryAdapter(getActivity(), null, false);
        mGallery.setAdapter(mAdapter);
        cellWidthAndHeight = AppUtil.getScreenWidth(getActivity()) / 4;
        getLoaderManager().initLoader(THUMBNAIL_LOAD, null, this);
        mBack.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
        getLoaderManager().destroyLoader(THUMBNAIL_LOAD);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        if (!isDetached()) {
            if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }
            mLoadingDialog = new ProcessDialog(getActivity());
            mLoadingDialog.showMessage("正在加载相册");
            switch (i) {
                case THUMBNAIL_LOAD:
                    final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
                            MediaStore.Images.ImageColumns.ORIENTATION};
                    return new CursorLoader(AppUtil.getAppContext(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, MediaStore.Images.Media.DATE_TAKEN + " DESC");
            }
        }
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.changeCursor(cursor);
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        if (isVisible() && getActivity() != null && !getActivity().isFinishing()) {
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
            }
            mLoadingDialog = new ProcessDialog(getActivity());
            mLoadingDialog.showMessage("正在加载相册");
        }
        mAdapter.changeCursor(null);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wdb_back:
                getActivity().finish();
                break;
        }
    }


    private class GalleryAdapter extends CursorAdapter {

        public GalleryAdapter(Context context, Cursor c, boolean autoRequery) {
            super(context, c, autoRequery);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {

            View contentView = View.inflate(context, R.layout.wdb_gallery_item, null);

            ViewHolder holder = new ViewHolder();

            holder.picture = (SimpleDraweeView) contentView.findViewById(R.id.wdb_picture);

            contentView.setTag(holder);

            return contentView;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            try {
                if (cursor != null) {
                    ViewHolder holder = (ViewHolder) view.getTag();
                    if (holder != null) {
                        int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                        if (dataColumnIndex < cursor.getColumnCount()) {
                            final String originalUrl = cursor.getString(dataColumnIndex);
                            final String url = new StringBuilder("file://").append(originalUrl).toString();
                            holder.picture.setAspectRatio(1.0f);
                            if (holder.picture.getLayoutParams().width != cellWidthAndHeight) {
                                holder.picture.getLayoutParams().width = cellWidthAndHeight;
                            }
                            if (holder.picture.getLayoutParams().height != cellWidthAndHeight) {
                                holder.picture.getLayoutParams().height = cellWidthAndHeight;
                            }
                            Object oldImageUrl = holder.picture.getTag();
                            if (oldImageUrl == null || !oldImageUrl.equals(url)) {
                                holder.picture.setTag(url);
                                FrescoImageLoader.bindCommonPic(holder.picture, url, cellWidthAndHeight, cellWidthAndHeight);
                            }
                            holder.picture.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent data = new Intent();
                                    data.putExtra(GalleryActivity.CHOOSE_IAMGE_URL, originalUrl);
                                    getActivity().setResult(Activity.RESULT_OK, data);
                                    getActivity().finish();
                                }
                            });
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private static class ViewHolder {
        public SimpleDraweeView picture;
    }
}


package com.koudai.weidian.buyer.image.loader;

import android.net.Uri;
import android.view.ViewGroup;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by zhaoyu on 15/5/11.
 */
public final class FrescoImageLoader {

    public static void bindCommonPic(SimpleDraweeView view, String uri) {
        bindCommonPic(view, uri, 0, 0);
    }

    public static void bindCommonPic(SimpleDraweeView view, String uri, int width, int height) {
        if (width <= 0) {
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp != null && lp.width > 0) {
                width = lp.width;
            }
        }

        if (height <= 0) {
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp != null && lp.height > 0) {
                height = lp.height;
            }
        }

        ImageRequest imageRequest =
                ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri))
                        .setResizeOptions(
                                new ResizeOptions(width, height))
                        .setAutoRotateEnabled(false)
                        .build();
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setImageRequest(imageRequest)
                .setOldController(view.getController())
                .build();
        view.setController(draweeController);
    }
}

KrystalJake1989 avatar May 12 '15 13:05 KrystalJake1989

Hmm...what version of Android are you using?

Do you have any errors in logcat that might be related? We'd expect to see something here.

IanChilds avatar May 12 '15 13:05 IanChilds

android4.4 and I don't find any ERROR level logs.Can you write a demo which reads phone album?Then you will meet the same problem.

KrystalJake1989 avatar May 12 '15 14:05 KrystalJake1989

By the way,this problem doesn't occurs on my nexus6

KrystalJake1989 avatar May 12 '15 14:05 KrystalJake1989

How long will it take you to fix this bug.I need to publish my app next week?Or can you give me some suggestions? Emergence!!Please help,thanks a lot!!

KrystalJake1989 avatar May 13 '15 01:05 KrystalJake1989

Unfortunately we are not currently sure what is causing this issue, and we don't have time for an in-depth investigation right now.

I suggest you try adding a listener to your drawee controller, and maybe to the image pipeline too, that will enable you to help debug where the images are getting lost.

What format are your images? We currently only resize JPEG so perhaps that is causing a problem?

IanChilds avatar May 13 '15 08:05 IanChilds

你是口袋购物的吗?

xianfeng-zhu avatar May 13 '15 08:05 xianfeng-zhu

是的,最近也在研究fresco,想用在项目里,不过看来貌似需要看看源码自己改改了,有兴趣交个朋友一起研究吗?

KrystalJake1989 avatar May 13 '15 08:05 KrystalJake1989

@lanchilds ok,I try to investigate the problem by myself and hope to discuss with you in future,if I found the reason.thanks a lot

KrystalJake1989 avatar May 13 '15 08:05 KrystalJake1989

@KrystalJake1989 你解决了这个问题?

sangmingming avatar May 14 '15 05:05 sangmingming

@KrystalJake1989 问题解决了吗

lixiao000018 avatar May 14 '15 07:05 lixiao000018

get same problem. is resolve?

agoodcoolman avatar May 22 '15 08:05 agoodcoolman

@agoodcoolman It has been resolved

lixiao000018 avatar May 22 '15 09:05 lixiao000018

@lixiao000018,how to solve it.I find even if i set resize params,but if you read image from sdcard,it has no effect.So it causes TooManyBitmapsException

KrystalJake1989 avatar May 22 '15 09:05 KrystalJake1989

@KrystalJake1989
ImageRequest imageRequest=ImageRequestBuilder.newBuilderWithSource(Uri.parse("file://"+url)).setLocalThumbnailPreviewsEnabled(true).setResizeOptions(new ResizeOptions(150,150)).build(); DraweeController draweeController = Fresco.newDraweeControllerBuilder() .setImageRequest(imageRequest) .build(); view.setController(draweeController); Note ResizeOptions parameters and not too small

lixiao000018 avatar May 23 '15 12:05 lixiao000018

@lixiao000018,I think the root reason why I can't load lots of bitmaps from sdcard is that if images are png format,fresco doesn't support resize,then each image is so big in memory and BitmapCounter class has a limit on how much memory the bitmaps can occupy in total.So If I loads a lot of png image in time,then it causes TooManyBitmapsException,then most of image can not be displayed

KrystalJake1989 avatar May 25 '15 04:05 KrystalJake1989