Photo Album Android

Asked

Viewed 37 times

0

I’m putting together an album, and with the code below, I can see all the images:

@Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        view = getLayoutInflater().inflate(R.layout.grid_item_layout , viewGroup, false);
        ImageView image = (ImageView) view.findViewById(R.id.image);
        image.setImageURI(Uri.parse(getItem(position).toString()));

        return view;
    }

inserir a descrição da imagem aqui

Only this way, the memory device runs out too fast. So I found two Apis that were supposed to do the same job: Picasso and Glide. But when using these lines, images do not appear in Imageview.

@Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        view = getLayoutInflater().inflate(R.layout.grid_item_layout , viewGroup, false);
        ImageView image = (ImageView) view.findViewById(R.id.image);
        //Nenhuma das duas abaixo funciona
        //Picasso.with(getApplication()).load(Uri.parse(getItem(position).toString())).into(image);
        //Glide.with(GaleriaActivity.this).load(Uri.parse(getItem(position).toString())).into(image);

        return view;
    }

Upshot:

inserir a descrição da imagem aqui

How can I fix this?

2 answers

0


The mistake is that you are using Uri.parse. You should actually enter the URL directly as a method parameter .load() in Glide or Picasso. See below for the correct information:

Glide.with(this)
    .load("https://www.w3schools.com/css/paris.jpg")
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .into(ivImgGlide);

So since you’re passing a list of Urls, I assume, you should do it this way:

Glide.with(this)
        .load(getItem(position).toString())
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .into(ivImgGlide);
  • It worked, vlw by the help.

  • 1

    @Eversond.Ferreira I’ll give a +1 on your question, I don’t want it to be negative. Other people can go through the same problem.

0

Try:

Uri uri = Uri.fromFile(new File(getItem(position).toString()));
Glide.with(context).load(uri).into(image);

A tip:

Inflating a layout for each Adapter item can become costly and make the scroll not smooth. I recommend using the pattern Viewholder

Browser other questions tagged

You are not signed in. Login or sign up in order to post.