Androidstudio - Swap url for image in local project path

Asked

Viewed 66 times

-1

Good afternoon, everyone,

I have a string that I use to set the image of some buttons:

 String imagem[] = {"https://cdn.pixabay.com/photo/2015/12121.jpg",
            "https://cdn.pixabay.com/photo/2016/0555555.jpg"};

I downloaded the images and saved in the src main images directory inside the project.

How can I replace the url in the string above with the local path?

Follow the detailed code:

In my onCreateView:

mAdapter = new ModuloAdapter(lista(), getActivity());

private List<ItemModulo> lista() {

    String titulo[] = {"A", "B", "C", "D"};

    String imagem[] = {"https://cdn.pixabay.com/photo/2015/10/19/20/01/5555.jpg",
              "https://cdn.pixabay.com/photo/2016/02/11/23/24/5554.jpg",
              "https://cdn.pixabay.com/photo/2016/11/08/05/29/44141.jpg",
              "https://cdn.pixabay.com/photo/2016/11/08/05/29/4412241.jpg"};

    for (int i = 0; i < titulo.length; i++){
            ItemModulo item = new ItemModulo();
            item.setLista(lista[i]);
            item.setImagem(imagem[i]);
            itemList.add(item);
    }

    return itemList;
}
  • Details more how you are setting the image on the buttons

  • I edited in more detail

  • Still kind of confused, what you want is that at runtime the application will download the images pro appliance, and these saved images will be used on the button?

  • not I will use the image already downloaded and placed in drawable but I am not able to use the direct image only with the link.... would like to place the image path in the string instead of the url

1 answer

0

Hello, the right is to leave project images in the drawable folder and change its nomenclature not to start with number:

src/main/res/drawable

You can call the images as follows:

        //adiciona suas imagens em um array de inteiros ao invés de String
        int[] imagem = new int[]{R.drawable.imagem1, R.drawable.imagem2, R.drawable.imagem3};

Soon, in your getters and setters changes the image and getImage to int

in his for to send the list keeps the same

Now here’s the trick, when it comes to retrieving that entire image do the following:

Drawable drawable = getResources().getDrawable(item.getImagens());

Use this drawable to set the image on the button.

The R class is responsible for communication between xml and java, and the drawable directory where the project images are stored.

  • I hope you helped him, any questions just talk, and if the answer was useful to you, mark it as useful or as answered. Hug

  • Thanks for the reply friend. but I could not make it work. brings the image empty. I use the string as follows I have an array with the names and the array up there with the images. The image is in drawable for (int i = 0; i<type.length; i++){ Itemmodulo item = new Itemmodulo(); item.setRecipe(type[i]); item.setImg(img[i]); itemList.add(item); }

  • @user147627 I edited my answer, if you still can’t get it, edit your question and put how you’re getting this image in getImage and sending it to the button. I did a simple project with 3 different examples with the same result, if you want to compare with your code let me know that I put it on github and I am in charge of my answer.

Browser other questions tagged

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