android 工作室加载位图失败

android studio failed to load bitmap

大家好,我的应用程序有问题,它总是显示内存不足或位图分配失败的错误。我认为由于图像较大,我有一个 recyclerview、cardview 和一个 gridview 我认为我的问题的答案是使用像 glidepicasso 这样的图像加载器,但我不知道如何将它输入到我的代码中,我的 onbindViewholder 中没有 get.(position) 来输入我的滑行,我所有的图像都是可绘制的,而不是 URL 请帮助我。我在 Youtube 上找到了它,这是一个论文项目,这是我的代码:

TraceMyAdapter

public class TraceMyAdapter extends RecyclerView.Adapter<TraceMyHolder> {
    Context c;
    String[] alphabet;
    int[] images;

    public TraceMyAdapter(Context ctx, String[] alphabet, int[] images){
        this.c = ctx;
        this.alphabet = alphabet;
        this.images = images;
    }

    @Override
    public TraceMyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.trace_cardview, null);
        TraceMyHolder holder = new TraceMyHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(TraceMyHolder holder, final int position) {
        int s = holder.getAdapterPosition();

        holder.img.setImageResource(images[position]);

        holder.alphabetTxt.setText(alphabet[position]);

        holder.setItemClickListener(new TraceItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {
                Intent i=new Intent(c,Trace_view.class);

                i.putExtra("Letters",alphabet[position]);
                i.putExtra("Images",images[position]);
                c.startActivity(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return alphabet.length;
    }
}

追踪菜单

public class Tracing_menu extends AppCompatActivity {
    String[] names = {
            "Letter A",
            "Letter B",
            "Letter C",
            "Letter D",
            "Letter E",
            "Letter F",
            "Letter G"};

    int[] images = {
            R.drawable.bga,
            R.drawable.bgb,
            R.drawable.bgc,
            R.drawable.bgd,
            R.drawable.bge,
            R.drawable.bgf,
            R.drawable.bgg
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tracing_menu);

        RecyclerView rv = findViewById(R.id.myRecycler);

        rv.setLayoutManager(new GridLayoutManager(this, 3));
        rv.setItemAnimator(new DefaultItemAnimator());

        TraceMyAdapter adapter = new TraceMyAdapter(this, names, images);
        rv.setAdapter(adapter);
    }
}

我的持有人

public class TraceMyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    ImageView img;
    TextView alphabetTxt;
    private TraceItemClickListener TraceitemClickListener;

    public TraceMyHolder(View itemView) {
        super(itemView);

        img = (ImageView) itemView.findViewById(R.id.img_alphabet);
        alphabetTxt = (TextView) itemView.findViewById(R.id.txt_alphabet);

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        this.TraceitemClickListener.onItemClick(view, getLayoutPosition());
    }

    public void setItemClickListener(TraceItemClickListener ic){
        this.TraceitemClickListener = ic;
    }
}

您可以使用以下语法将可绘制资源 ID 传入 glide:

Glide.with(context).load(images[position]).into(holder.img);

考虑到您 运行 遇到很多 OOM 错误,我会建议这条路线,因为 Glide 将有助于管理下采样。

我建议您看一下这些可绘制对象的图像大小。它们可能比需要的更大。 Glide 会有所帮助,但最好的办法是为不同的可绘制桶提供正确大小的图像。请务必查看 Supporting different screen sizes android 文档。注意密度桶和图像大小乘数。

glide docs 将其放入 gradle。

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.6.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
}

在你的 onBindViewHolder

@Override
public void onBindViewHolder(TraceMyHolder holder, final int position) 
{
int s = holder.getAdapterPosition();



Glide.with(ctx).load(images[position]).into(holder.img);


holder.alphabetTxt.setText(alphabet[position]);

holder.setItemClickListener(new TraceItemClickListener() {
    @Override
    public void onItemClick(View v, int pos) {
        Intent i=new Intent(c,Trace_view.class);

        i.putExtra("Letters",alphabet[position]);
        i.putExtra("Images",images[position]);


        c.startActivity(i);
    }
});
}