0

So I'm new to android and I'm currently stuck with an issue regarding dynamically generating images in my image view.

I have currently stored some images in my res/drawables folder under different sub-folders. So the file structure looks like this

 Drawables/
     german_shepherd/
           gs_1.jpg
           gs_2.jpg
           gs_3.jpg
     boxer/
           boxer_1.jpg
           boxer_2.jpg
           boxer_3.jpg

My app works such that first a random breed is selected, the program will look into the designated folder for that breed and then randomly pick one image from that list of image files.

I'm trying to load that randomly picked image into an Image View

I've tried a bunch of solutions from other stack overflow results but no matter what I tried whether it be using a BitMap or Drawable, the image still does not show up on my screen.

String[] breeds = getResources().getStringArray(R.array.breeds);
        randInt = (int)(Math.round(Math.random() * 20));

        String folderPath = "res/drawable/";
        String filePath = "";

        switch (breeds[randInt]){

            case "Australian Terrier":
                Log.i("Breed", breeds[randInt]);
                filePath = folderPath+"at/at_"+randInt+".jpg";
                Log.i("Image", filePath);

            case "Beagle":
                Log.i("Breed", breeds[randInt]);
                filePath = folderPath+"beagles/b_"+randInt+".jpg";
                Log.i("Image", filePath);
                break;

            case "Boxer":
                Log.i("Breed", breeds[randInt]);
                filePath = folderPath+"boxer/boxer_"+randInt+".jpg";
                Log.i("Image", filePath);
                break;

            case "Chihuahua":
                Log.i("Breed", breeds[randInt]);
                filePath = folderPath+"chihuahua/chihuahua_"+randInt+".jpg";
                Log.i("Image", filePath);
                break;

            case "Cockerspaniel":
                Log.i("Breed", breeds[randInt]);
                filePath = folderPath+"cocker_spaniel/cs_"+randInt+".jpg";
                Log.i("Image", filePath);
                break;
            default:
                Log.i("Error", "Not Found");

        }

        final ImageView imageView = findViewById(R.id.questionImage);
        Drawable drawable = Drawable.createFromPath(filePath);
        imageView.setImageDrawable(drawable);

So far this is the code I initially started with. I will also link the pages I checked for solutions in.

AiSirachcha21
  • 119
  • 14

2 Answers2

1

The resources mechanism doesn't support subfolders in the drawable directory. You cannot create subfolders under the res/drawable package.

Hence, no matter what you try with the above logic, your code wont execute to give you the desired result.

What you can do is:

Access your Internal storage/External storage -> create separate folder under Pictures directory of your user devices -> Create subfolder -> store those images under their specific folder -> Perform your logic by accessing them from the Internal storage and not from the res/drawable/[folder_name] structure.

Update:

You mentioned you have static images with you and not the images that images are available on any other server. So, add those images into the drawable folder. Then with your code access file manager and add those images into the users pictures folder under specific sub-folder of your need. Then where u want to show them, call from those specific subfolder from file manager - Pictures directory of device.

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
  • So your solution is that I have to download these images into the device I'm working with during installation and then refer to the directory made inside the device to work with the folder? – AiSirachcha21 Feb 23 '20 at 12:55
0

you can use different folders for your drawables image by following the below steps,

Create your Res Folders

  1. create a folder in resources called drawables with "s"
  2. create new sub-folder by selecting the new folder and right click->New->Folder->Res Folder (name the first one german_shepherd etc ...) enter image description here

after you have created your sub-folders as Res Folders, now you need to add a folder called drawable inside each one. your final res folder should look like this

enter image description here

  1. The last step is to re-Sync your app/build.gradle file.you should see the below block added inside android( your gradle may not add it automatically in this case you should add it manually)

    sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/drawables/boxer', 'src/main/res/drawables/german_shepherd', ] } }

use resource in code

After all this steps you should be able to set your images simply by calling setImageDrawable imageView.setImageDrawable(R.drawable.image1);

Explanation what this is doing basically is setting multiple resources so the compiler will recognize them and add them to your R.class

Chamlal
  • 156
  • 1
  • 5
  • Okay but does that mean I can store my image files in it? No right? – AiSirachcha21 Feb 23 '20 at 15:24
  • yes you can store your images in it and use the R.drawable.imageName to call them, but pay attention that the compiler will merge all your sub-folders into one big drawable java resource ( R.drawable) so you must use unique names even in sub-folders, but beside that you can use your images – Chamlal Feb 23 '20 at 15:32