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.
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.