Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 图像到整数_Android - Fatal编程技术网

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);

谢谢你,伙计。这就是答案。我忘了“拖拉”的论点了。谢谢你。答案就是这样。我忘了“可画”的论点。