Android 图像到整数
如何将图像资源转换为整数? 我用了这个:Android 图像到整数,android,Android,如何将图像资源转换为整数? 我用了这个: int rh=this.getResources().getIdentifier("ic_launcher", null, this.getPackageName()); 但它不起作用。 我对此进行了日志记录,并且相对湿度的值始终为0。 有没有其他方法可以将图像转换为整数?试试这个 int id = getResources().getIdentifier("ic_launcher","drawable", getPackageName()); 它将
int rh=this.getResources().getIdentifier("ic_launcher", null, this.getPackageName());
但它不起作用。
我对此进行了日志记录,并且相对湿度的值始终为0。
有没有其他方法可以将图像转换为整数?试试这个
int id = getResources().getIdentifier("ic_launcher","drawable", getPackageName());
它将返回整数资源id。试试这个
int id = getResources().getIdentifier("ic_launcher","drawable", getPackageName());
它将返回整数形式的资源id。您的任务如下:
int i = this.getResources().getIdentifier("ic_launcher", "strings", this.getPackageName());
或
其中1个应该可以工作。您的:
int i = this.getResources().getIdentifier("ic_launcher", "strings", this.getPackageName());
或
其中1个应该可以工作。请尝试此指定类型:
int rh=this.getResources().getIdentifier("ic_launcher", "drawable", this.getPackageName());
请记住,它并不是将图像转换为整数,
getIdentifier()
只是查找用于查找打包在.apk文件中的资源的资源id。请尝试以下指定类型:
int rh=this.getResources().getIdentifier("ic_launcher", "drawable", this.getPackageName());
请记住,它并不是将图像转换为整数,
getIdentifier()
只是查找用于查找打包在.apk文件中的资源的资源id。如果所有其他方法都不起作用,请尝试以下代码:
public static int getResId(String variableName, Class<?> c) {
Field field = null;
int resId = 0;
try {
field = c.getField(variableName);
try {
resId = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return resId;
哪里???是正确的可绘制文件夹。如果所有其他方法都无效,请尝试以下代码:
public static int getResId(String variableName, Class<?> c) {
Field field = null;
int resId = 0;
try {
field = c.getField(variableName);
try {
resId = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return resId;
哪里???是正确的可绘制文件夹。您也可以在不使用资源的情况下实现这一点
int id=R.drawable.icon;
在这里,图标是可绘图文件夹中的图像
将此id
值设置为整数
ImageView imageView=null;
imageView.setImageResource(id);
您也可以在不使用资源的情况下实现这一点
int id=R.drawable.icon;
在这里,图标是可绘图文件夹中的图像
将此id
值设置为整数
ImageView imageView=null;
imageView.setImageResource(id);
谢谢你,伙计。这就是答案。我忘了“拖拉”的论点了。谢谢你。答案就是这样。我忘了“可画”的论点。