gpt4 book ai didi

java - 我需要从存储在我的 res/drawables 中的图像将图像设置到我的 ImageView 中

转载 作者:行者123 更新时间:2023-12-02 08:58:13 27 4
gpt4 key购买 nike

所以我是 Android 新手,目前遇到了有关在 ImageView 中动态生成图像的问题。

我目前已将一些图像存储在不同子文件夹下的 res/drawables 文件夹中。所以文件结构看起来像这样

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

我的应用程序的工作原理是,首先选择一个随机品种,程序将查找该品种的指定文件夹,然后从该图像文件列表中随机选择一个图像。

我正在尝试将随机选取的图像加载到 ImageView

我已经尝试了许多来自其他堆栈溢出结果的解决方案,但无论我尝试什么,无论是使用 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] 结构访问它们。

更新:

您提到您有静态图像,而不是任何其他服务器上可用的图像。因此,将这些图像添加到可绘制文件夹中。然后使用您的代码访问文件管理器并将这些图像添加到您需要的特定子文件夹下的用户图片文件夹中。然后,在您想要显示它们的地方,从文件管理器 - 设备的图片目录中的特定子文件夹中调用。

关于java - 我需要从存储在我的 res/drawables 中的图像将图像设置到我的 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60362130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com