【问题标题】:android java.lang.OutOfMemoryError recyclerview with lots of images带有大量图像的android java.lang.OutOfMemoryError recyclerview
【发布时间】:2016-04-17 12:38:48
【问题描述】:

我正在尝试制作一个可以显示大量图像的应用。问题是当我运行应用程序和滚动时,应用程序崩溃并显示错误

java.lang.OutOfMemoryError.

我使用的是 android studio,minSdkVersion 15,所有图片都不大于 20、30 KB,但我会展示很多图片。所有图片都在抽屉文件夹中。

这是我将所有数据添加到包括图像在内的对象的类:

package com.example.android.animalsoundsforchildren;

import java.util.ArrayList;

public class Zivotinje {
    private String mAnimal_name;
    private int mAnimal_slika;
    private int mSound;
    private String mkey;

    public Zivotinje(String animalName, int slika, int sound, String key){
        this.setmAnimal_name(animalName);
        this.setmAnimal_slika(slika);
        this.setmSound(sound);
        this.setMkey(key);
    }

    public String getmAnimal_name() {
        return mAnimal_name;
    }

    public void setmAnimal_name(String mAnimal_name) {
        this.mAnimal_name = mAnimal_name;
    }

    public int getmAnimal_slika() {
        return mAnimal_slika;
    }

    public void setmAnimal_slika(int mAnimal_slika) {
        this.mAnimal_slika = mAnimal_slika;
    }
    public int getmSound() {
        return mSound;
    }

    public void setmSound(int mSound) {
        this.mSound = mSound;
    }


    public String getMkey() {
        return mkey;
    }

    public void setMkey(String mkey) {
        this.mkey = mkey;
    }
    //static
    public static ArrayList<Zivotinje> createZivotinjeList(){
        ArrayList<Zivotinje> zivotinje = new ArrayList<Zivotinje>();

        zivotinje.add(new Zivotinje("Sheep", R.drawable.domestic_sheep, R.raw.domestic_sheep, "Domestic"));

        //ADING DATA TO ARRAY OF OBJECTS
        zivotinje.add(new Zivotinje("Sparrow", R.drawable.birds_sparrow, R.raw.birds_sparrow, "Birds"));
        zivotinje.add(new Zivotinje("Flamingo", R.drawable.birds_flamingo, R.raw.birds_flamingo, "Birds"));
        zivotinje.add(new Zivotinje("Hyacinth Macaw", R.drawable.birds_hyacinth_macaw, R.raw.birds_hyacinth_macaw, "Birds"));
        zivotinje.add(new Zivotinje("Kestrel", R.drawable.birds_kestrel, R.raw.birds_kestrel, "Birds"));
        zivotinje.add(new Zivotinje("Pigeon", R.drawable.birds_pigeon, R.raw.birds_pigeon, "Birds"));
        zivotinje.add(new Zivotinje("Seagull", R.drawable.birds_seagull, R.raw.birds_seagull, "Birds"));
        zivotinje.add(new Zivotinje("Toucan", R.drawable.birds_toucan, R.raw.birds_toucan, "Birds"));
        zivotinje.add(new Zivotinje("Swallow", R.drawable.birds_swallow, R.raw.birds_swallow, "Birds"));

        zivotinje.add(new Zivotinje("Dolphin", R.drawable.sea_dolphin, R.raw.sea_dolphin, "Sea"));
        zivotinje.add(new Zivotinje("Bumblebee", R.drawable.insects_bumblebee, R.raw.insects_bumblebee, "Insects"));
        zivotinje.add(new Zivotinje("Tractor", R.drawable.cars_tractor, R.raw.cars_tractor, "Cars"));
        zivotinje.add(new Zivotinje("Baby Cry", R.drawable.laugh_baby_cry, R.raw.laugh_baby_cray, "Laugh"));
        zivotinje.add(new Zivotinje("Wind", R.drawable.nature_wind, R.raw.nature_wind, "Nature"));
        zivotinje.add(new Zivotinje("Golf", R.drawable.effects_golf, R.raw.effects_golf, "Effects"));
        zivotinje.add(new Zivotinje("Clock", R.drawable.others_clock, R.raw.others_clock, "Others"));

        return zivotinje;
    }

    //Dohvacamo sve zivotinje i sortiramo specificne po kljucu tako da kad kliknemo recimo others pozove se ovo
    public static ArrayList<Zivotinje> createZivotinjeListPero(String zivotinjeMenu) {
        ArrayList<Zivotinje> zivotinje = new ArrayList<Zivotinje>();
        ArrayList<Zivotinje> zivotinjeLista = new ArrayList<Zivotinje>();

        zivotinje = createZivotinjeList();


        for (Zivotinje ziv : zivotinje) {

            if (ziv.mkey == zivotinjeMenu) {
                zivotinjeLista.add(ziv);
            }
        }
        return zivotinjeLista;
    }

}

这是适配器,我想我需要处理 onbindviewholder 方法中的问题但我不知道如何:

package com.example.android.animalsoundsforchildren;

import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {

    @Override
    public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(contactView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) {

        Zivotinje zivotinjeIme = mZivotinje.get(position);

        // Set item views based on the data model
        TextView textView = viewHolder.nameTextView;
        textView.setText(zivotinjeIme.getmAnimal_name());

        //THIS IS WHERE I HANDALE IMAGE VIEW
        ImageView slika_source = viewHolder.imageView;
        slika_source.setImageResource(zivotinjeIme.getmAnimal_slika());


        viewHolder.setItem(mZivotinje.get(position));



    }


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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public TextView nameTextView;
        private Zivotinje mItem;



        public void setItem(Zivotinje item) {
            this.mItem = item;
        }



        public ViewHolder(final View itemView) {
            super(itemView);

            nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id);
            imageView = (ImageView) itemView.findViewById(R.id.slika_id);







            itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), mItem.getmSound());
                    mediaPlayer.start();
                }
            });

        }
    }

    private List<Zivotinje> mZivotinje;

    public ZivotinjeAdapter(List<Zivotinje> animals) {
        mZivotinje = animals;
    }
}

【问题讨论】:

  • public static ArrayList createZivotinjeList(){public static ArrayList createZivotinjeListPero(String zivotinjeMenu) { 尝试在 MainActivity.class 的 OnCreate 中调用它

标签: android performance nullpointerexception android-recyclerview


【解决方案1】:

你需要使用图片加载库来加载图片,

由于图像的分辨率,您获得了 OOM。

有很多图片加载库,但我更喜欢Glide。它在很多方面都比其他人好,

它会解决你的OOM问题

这里是用法

 ImageView slika_source = viewHolder.imageView;
 Glide.with(mContext)
            .load((Integer) zivotinjeIme.getmAnimal_slika())                
            .into(slika_source);

【讨论】:

  • 我会尝试学习如何做到这一点。我不是android的专业人士,最近才开始。希望不会很难。
  • 我只是做这一切,我导入库。现在好一点了。这次我可以向下滚动,但是当我往回滚动时它又崩溃了。这就是我在 oncreateview 方法中所做的
  • 它现在可以工作了,我没有把你的代码放在正确的地方。谢谢你
【解决方案2】:

大多数情况下,在加载大量位图(图像)时,会尝试使用第三方工具/库,因为它们是在正确的内存管理、内存清除所有概念的基础上工作的......这是非常困难的任务。

尝试使用...

1)。通用图像加载器

2)。毕加索(官方链接:http://square.github.io/picasso/

3)。 Glide(Github 链接:https://github.com/bumptech/glide)

有了这些,别忘了在项目的清单文件中添加权限...

android:hardwareAccelerated="true" 和 android:largeHeap="true"

【讨论】:

    【解决方案3】:

    您可以在AndroidManifest.xml 中使用android:largeHeap="true"

    <application
        android:largeHeap="true">
    

    【讨论】:

    • 谢谢。我只是尝试一下,应用程序仍然崩溃
    【解决方案4】:

    将此添加到您的代码中

    mRecyclerView.setItemViewCacheSize(3);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-23
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      相关资源
      最近更新 更多