因此,我是android的新手,我目前仍然遇到有关在图像视图中动态生成图像的问题。
我目前在一些子文件夹下的res / drawables文件夹中存储了一些图像。所以文件结构看起来像这样
Drawables/
german_shepherd/
gs_1.jpg
gs_2.jpg
gs_3.jpg
boxer/
boxer_1.jpg
boxer_2.jpg
boxer_3.jpg
我的应用程序的工作方式是,首先选择一个随机品种,程序将查找该品种的指定文件夹,然后从该图像文件列表中随机选择一个图像。
我正在尝试将随机选择的图像加载到图像视图
我已经尝试了其他堆栈溢出结果中的许多解决方案,但是无论我尝试使用BitMap还是Drawable,图像仍然不会显示在屏幕上。
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);
到目前为止,这是我最初开始使用的代码。我还将链接我检查过的解决方案的页面。
资源机制不支持drawable目录中的子文件夹。您不能在res / drawable包下创建子文件夹。
因此,无论您如何尝试上述逻辑,您的代码都不会执行以提供所需的结果。
您可以做的是:
访问内部存储器/外部存储器->在用户设备的图片目录下创建一个单独的文件夹->创建子文件夹->将这些图像存储在其特定的文件夹下->通过从内部存储器访问它们并执行不是来自res / drawable / [folder_name]结构。
更新:
您提到您拥有静态图像,而不是该图像在其他任何服务器上都可用。因此,将这些图像添加到drawable文件夹中。然后,使用您的代码访问文件管理器,将这些图像添加到您需要的特定子文件夹下的用户图片文件夹中。然后在您要显示它们的位置,从文件管理器的特定子文件夹中调用-设备的图片目录。