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.