Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KrystalJake1989 opened this issue May 12, 2015 · 28 comments
Closed
Labels

Comments

@KrystalJake1989
Copy link

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

@sangmingming
Copy link

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

@KrystalJake1989
Copy link
Author

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

@KrystalJake1989
Copy link
Author

I find this constant MAX_SIMULTANEOUS_FILE_FETCH_AND_RESIZE in ProducerSequenceFactory

@IanChilds
Copy link
Contributor

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

@KrystalJake1989
Copy link
Author

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);
    }
}

@IanChilds
Copy link
Contributor

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.

@KrystalJake1989
Copy link
Author

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
Copy link
Author

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

@IanChilds IanChilds added the bug label May 12, 2015
@KrystalJake1989
Copy link
Author

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!!

@IanChilds
Copy link
Contributor

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?

@xianfeng-zhu
Copy link

你是口袋购物的吗?

@KrystalJake1989
Copy link
Author

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

@KrystalJake1989
Copy link
Author

@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

@sangmingming
Copy link

@KrystalJake1989 你解决了这个问题?

@lixiao000018
Copy link

@KrystalJake1989 问题解决了吗

@agoodcoolman
Copy link

get same problem. is resolve?

@lixiao000018
Copy link

@agoodcoolman It has been resolved

@KrystalJake1989
Copy link
Author

@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

@lixiao000018
Copy link

@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

@KrystalJake1989
Copy link
Author

@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

@lixiao000018
Copy link

@KrystalJake1989 We are not experiencing the same problem.

@KrystalJake1989
Copy link
Author

@lixiao000018,Do you load images from sdcard and Are they png format?

@agoodcoolman
Copy link

@lixiao000018 it's work?

@tyronen
Copy link
Contributor

tyronen commented Jul 20, 2015

There were issues with loading large numbers of images at once. Things should be better in v0.6.0.

@tyronen tyronen closed this as completed Jul 20, 2015
@QiuSean
Copy link

QiuSean commented Jul 23, 2015

我也遇到这个问题,必须resize时候hardcode宽高,有高手解决了么

@s1rius s1rius mentioned this issue Aug 22, 2015
@binqiangsun
Copy link

哈哈,好像是刚用fresco都会碰到的一个问题嘛......目前用resize解决。。。

@binqiangsun
Copy link

@KrystalJake1989 现在项目里用了么?可以交流一下么?

@keanulee0101
Copy link

sorry, found the comment that this issue is solved in
PNG issue #567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants