OutOfMemory异常 - 用于加载picasso图像时的adapter.notifiyDataSetChanged()

时间:2016-12-06 06:31:29

标签: android out-of-memory picasso

我在使用Picasso加载图片时遇到outOfMemory异常。 我用OkHttp创建了毕加索构建器并创建了Picasso Singleton类来缓存图像。

方案: 我有100多个要加载的图像源。我正在拍摄集合中的图像,每组都有25个图像网址,我正在使用毕加索进行设置。我正在使用recyclerview,每当有新的图像集进来时我都在调用adapter.notifyDataSetChanged()。我正在加载第一组 - 没有问题,第二组和第三组在进行改造以获得下一组并添加到现有列表并调用 - adapter.notifyDataSetChanged()时。当我调用adapter.NotifyDataSetChanged()应用程序与outOfMemoryException崩溃时的第3集

BUT

当我加载所有3组75张图片时,我没有遇到任何问题。

代码: 申请类 - 我正在建造毕加索。

 Picasso.Builder builder = new Picasso.Builder(this)
            .memoryCache(new LruCache(24000));
    builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE));
    Picasso built = builder.build();
    built.setLoggingEnabled(true);

Picasso Singleton Class:

public class PicassoCache {

/**
 * Static Picasso Instance
 */
private static Picasso picassoInstance = null;

/**
 * PicassoCache Constructor
 *
 * @param context application Context
 */
private PicassoCache (Context context) {

    Downloader downloader   = new OkHttpDownloader(context, Integer.MAX_VALUE);
    Picasso.Builder builder = new Picasso.Builder(context);
    builder.downloader(downloader);

    picassoInstance = builder.build();
}

/**
 * Get Singleton Picasso Instance
 *
 * @param context application Context
 * @return Picasso instance
 */
public static Picasso getPicassoInstance (Context context) {

    if (picassoInstance == null) {

        new PicassoCache(context);
        return picassoInstance;
    }

    return picassoInstance;
}

}

使用Picasso设置/加载图像的代码。

  PicassoCache.getPicassoInstance(context).load(url).placeholder(R.mipmap.banner_placeholder).into(mView);

我正在更新现有列表的代码,以便在数据集发生更改时加载。

内部Retorfit OnSuccess消息:

    if (response.code() == 200) {

List<CampaignCard> newCampaigns = response.body().getCampaigns();


for (int i = 0; i < newCampaigns.size(); i++) {
    if (!campaignCards.contains(newCampaigns.get(i))) {
        campaignCards.add(newCampaigns.get(i));
    }
}

dashBoardAdapter.notifyDataSetChanged();

} else if (response.code() == Params.CODE_422) {
    Utils.ShowServiceErrorMessages(getActivity(), response);
} else if (response.code() == Params.CODE_401) {
    Utils.Logout(getActivity());
}

适配器类:

public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.DashboardViewHolder> implements View.OnClickListener {


    private static final int VIEW_TYPE_CAMPAIGN = 1;
    private static final int VIEW_TYPE_FEED = 2;

    DashboardViewHolder holder;


    protected List list;
    protected int viewTypeLayout;
    Context context;
    int position;

    Dashboard.DashboardActionList actionList;
    Map<String, SourceContent> mPreviewLinkMapper;
    ViewGroup parent;

    //Picasso p;
    public DashboardAdapter(List list, int viewTypeLayout) {
        this.list = list;
        this.viewTypeLayout = viewTypeLayout;
    }

    public DashboardAdapter(List list, int viewTypeLayout, Context context, Map<String, SourceContent> mPreviewLinkMapper) {
        this.list = list;
        this.viewTypeLayout = viewTypeLayout;
        this.mPreviewLinkMapper = mPreviewLinkMapper;
    }

    @Override
    public DashboardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_feed, parent, false);
        return new DashboardViewHolder(view, 2);

    }

    @Override
    public void onBindViewHolder(DashboardViewHolder holder, final int position) {
        BindFeedData(holder, position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }





    @SuppressWarnings("unchecked")
    private void BindFeedData(DashboardViewHolder holder, int position) {
        List<Feed> feeds = (List<Feed>) list;
        if (holder.mBannerImage != null) {
            if (feeds.get(position).getCampaign().getParticipation().getType().toLowerCase().contains("video")) {

                holder.mPlayIcon.setVisibility(View.VISIBLE);
                String url = feeds.get(position).getCampaign().getParticipation().getThumbnail_url();
                Utils.LoadImages(context, url, holder.mBannerImage, false);

            } else if (feeds.get(position).getCampaign().getParticipation().getType().toLowerCase().contains("gif")) {

                holder.mPlayIcon.setVisibility(View.GONE);
                String url = feeds.get(position).getCampaign().getParticipation().getPost_file();
                Utils.loadGif(context, url, holder.mBannerImage);

            } else if (feeds.get(position).getCampaign().getParticipation().getType().toLowerCase().contains("link") ||
                    feeds.get(position).getCampaign().getParticipation().getType().toLowerCase().contains("youtube")) {

                holder.mPlayIcon.setVisibility(View.GONE);
                String slug = feeds.get(position).getCampaign().getSlug();
                List<String> images = mPreviewLinkMapper.get(slug).getImages();
                Utils.LoadImages(context, images.get(0), holder.mBannerImage, false);

            } else {
                holder.mPlayIcon.setVisibility(View.GONE);
                holder.mBannerImage.setVisibility(View.VISIBLE);
                String url = feeds.get(position).getCampaign().getParticipation().getPost_file();
                Utils.LoadImages(context, url, holder.mBannerImage, false);
            }
        }

        if (holder.mBrandLogo != null) {
            Utils.LoadImages(context, feeds.get(position).getInfluencer().getProfile_picture_url(), holder.mBrandLogo, true);
        }


        holder.mTitle.setText(feeds.get(position).getInfluencer().getName());
        holder.mSubTitle.setText(feeds.get(position).getCampaign().getName());
        holder.mTime.setText(feeds.get(position).getCampaign().getTimestamp());
        holder.mDescription.setText(feeds.get(position).getCampaign().getParticipation().getPost_content());
        holder.mEngagement.setText(feeds.get(position).getCampaign().getParticipation().getMetrics().getEngagements());
        holder.mImpresion.setText(feeds.get(position).getCampaign().getParticipation().getMetrics().getImpressions());
    }



    }


    public static class DashboardViewHolder extends RecyclerView.ViewHolder {

        ImageView mBannerImage, mFacebook, mTwitter, mInstagram, mPlayIcon, mHotIcon, mLocationIcon;
        CircularImageView mBrandLogo;
        CustomTextViewRegular mDescription, mTime, mOption1, mOption2, mOption3;
        CustomTextViewDemi mTitle, mSubTitle, mImpresion, mEngagement;
        LinearLayout mDetailsLayout;

        LinearLayout mOptionLayout1, mOptionLayout2, mOptionLayout3;
        public ViewGroup dropPreview;

        TableRow mTableOptions;

        public DashboardViewHolder(View v, int viewtype) {
            super(v);
            InitFeedViews(v);
          }




        private void InitFeedViews(View v) {
            mTitle = (CustomTextViewDemi) v.findViewById(R.id.adapterHeaderLayoutTitle);
            mSubTitle = (CustomTextViewDemi) v.findViewById(R.id.adapterHeaderLayoutSubTitle);
            mBrandLogo = (CircularImageView) v.findViewById(R.id.adapterHeaderLayoutLogo);
            mTime = (CustomTextViewRegular) v.findViewById(R.id.adapterHeaderLayoutTime);

            mBannerImage = (ImageView) v.findViewById(R.id.adapterFeedBannerImage);
            mPlayIcon = (ImageView) v.findViewById(R.id.adapterFeedPlayIocn);

            mImpresion = (CustomTextViewDemi) v.findViewById(R.id.adapterFeedImpressions);
            mEngagement = (CustomTextViewDemi) v.findViewById(R.id.adapterFeedEngagements);
            mDescription = (CustomTextViewRegular) v.findViewById(R.id.adapterFeedDescription);

            dropPreview = (LinearLayout) v.findViewById(R.id.drop_preview);
        }

    }

}

2 个答案:

答案 0 :(得分:1)

你会面临内存不足的问题。毕加索图书馆的情况很多我建议你使用Glide库。 我有很多&#34;内存不足&#34;异常尝试了所有但最后当我使用滑行它运作良好。

答案 1 :(得分:0)

尝试使用Picasso库调整图像大小。 OutOfMemoryException将被解决。

为了更好地理解,请参阅以下链接: https://futurestud.io/tutorials/picasso-image-resizing-scaling-and-fit