如何从 Assets 文件夹中获取特定的图像资源?
How to get specific images Resource from Assets Folder?
我在assets文件夹下有8张不同的植物图片,我把这8张图片的assets文件名(对应路径,例如foxtail.png,orchid.png)缓存在数据库的assets目录下。 (加上其他信息)
我'在 RecyclerView 中显示 8 株植物。单击任何植物打开详细信息 Activity。 (传递保存在资产文件夹中的图像文件名,例如 foxtail.png)
如何在资产文件夹中选择与传递给 Detail Activity 的文件名相匹配的特定图像文件并将其设置为 ImageView?
使用资源可绘制对象 ID 在每个 ImageView/View 上设置标签,例如。 R.drawable.foxtail
例如。 imageView.setTag(R.drawable.foxtail)
或 view.setTag(R.drawable.foxtail)
选中一个后,获取标签发送给下一个activity:
然后再次检查例如
imageTag = getIntent().getIntExtra("chosenPlant");
if ( imageTag == R.drawable.foxtail ){
//Perform action if this pic was selected (foxtail.png)
newImageView.setImageResource(R.drawable.foxtail);
} else ...
您可以创建一个包含资源 ID 的 int 数组。
int images[]={
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3,
R.drawable.image_4,
R.drawable.image_5,
R.drawable.image_6,
R.drawable.image_7,
R.drawable.image_8
};
在您的数据库中存储与资源数组中图像位置对应的图像 ID。
| id | image_id | information |
-------------------------------
| 0 | 2 | info_0 |
| 1 | 0 | info_1 |
| 2 | 4 | info_2 |
所以当你从数据库中获取行时,你可以使用image_id从数组中检索相应的图像
ImageView imageView = (ImageView)v.findViewById(R.id.imageView);
imageView.setImageResource(images[image_id]);
您可以:
以流方式打开文件
InputStream imageStream = null;
try {
// get input stream
imageStream = getAssets().open("foxtail.png");
// load image as Drawable
Drawable drawable= Drawable.createFromStream(imageStream, null);
// set image to ImageView
image.setImageDrawable(drawable);
}
catch(IOException ex) {
return;
}
最后记得用
关闭流
if(imageStream !=null){
imageStream.close();
}
或
将图像移动到 res/drawable 文件夹中 您可以使用以下方式加载图像:
String yourImageName = getImageNameFromDB();
int resId= getResources().getIdentifier(yourImageName, "drawable", "com.example.yourpackegename.");
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(resId);
或
与 类似的东西(总是将图像放入 res/drawable):
private enum Plant {
foxtail, orchid, xyz;
}
String value = getPlantFromDB();
Plant plant = Plant.valueOf(value); // surround with try/catch
switch(plant) {
case foxtail :
resId= R.drawable.foxtail
break;
case orchid :
resId= R.drawable.orchid
break;
default :
resId= R.drawable.xyz
break;
Drawable drawable = getResources().getDrawable(resId);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(drawable);
我在assets文件夹下有8张不同的植物图片,我把这8张图片的assets文件名(对应路径,例如foxtail.png,orchid.png)缓存在数据库的assets目录下。 (加上其他信息)
我'在 RecyclerView 中显示 8 株植物。单击任何植物打开详细信息 Activity。 (传递保存在资产文件夹中的图像文件名,例如 foxtail.png)
如何在资产文件夹中选择与传递给 Detail Activity 的文件名相匹配的特定图像文件并将其设置为 ImageView?
使用资源可绘制对象 ID 在每个 ImageView/View 上设置标签,例如。 R.drawable.foxtail
例如。 imageView.setTag(R.drawable.foxtail)
或 view.setTag(R.drawable.foxtail)
选中一个后,获取标签发送给下一个activity:
然后再次检查例如
imageTag = getIntent().getIntExtra("chosenPlant");
if ( imageTag == R.drawable.foxtail ){
//Perform action if this pic was selected (foxtail.png)
newImageView.setImageResource(R.drawable.foxtail);
} else ...
您可以创建一个包含资源 ID 的 int 数组。
int images[]={
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3,
R.drawable.image_4,
R.drawable.image_5,
R.drawable.image_6,
R.drawable.image_7,
R.drawable.image_8
};
在您的数据库中存储与资源数组中图像位置对应的图像 ID。
| id | image_id | information |
-------------------------------
| 0 | 2 | info_0 |
| 1 | 0 | info_1 |
| 2 | 4 | info_2 |
所以当你从数据库中获取行时,你可以使用image_id从数组中检索相应的图像
ImageView imageView = (ImageView)v.findViewById(R.id.imageView);
imageView.setImageResource(images[image_id]);
您可以:
以流方式打开文件
InputStream imageStream = null;
try {
// get input stream
imageStream = getAssets().open("foxtail.png");
// load image as Drawable
Drawable drawable= Drawable.createFromStream(imageStream, null);
// set image to ImageView
image.setImageDrawable(drawable);
}
catch(IOException ex) {
return;
}
最后记得用
关闭流if(imageStream !=null){
imageStream.close();
}
或
将图像移动到 res/drawable 文件夹中 您可以使用以下方式加载图像:
String yourImageName = getImageNameFromDB();
int resId= getResources().getIdentifier(yourImageName, "drawable", "com.example.yourpackegename.");
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(resId);
或
与 类似的东西(总是将图像放入 res/drawable):
private enum Plant {
foxtail, orchid, xyz;
}
String value = getPlantFromDB();
Plant plant = Plant.valueOf(value); // surround with try/catch
switch(plant) {
case foxtail :
resId= R.drawable.foxtail
break;
case orchid :
resId= R.drawable.orchid
break;
default :
resId= R.drawable.xyz
break;
Drawable drawable = getResources().getDrawable(resId);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(drawable);