【发布时间】:2020-06-07 06:27:51
【问题描述】:
所以我是 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);
到目前为止,这是我最初开始使用的代码。我还将链接我检查解决方案的页面。
【问题讨论】:
标签: java android android-imageview