Android 中可绘制对象的 ID 是否发生变化?

Does the ID of a drawable changes in Android?

例如,我有一张图片 background1.jpg,这是获取 ID 的代码

int background = R.drawable.background1;

然后我上传int background到数据库。我关闭了应用程序并再次打开它然后我从之前保存的数据库中检索了 ID 并将其设置在 ImageView

int background = json.getInt("background");
image.setImageResource(background);

这可以吗? R.drawable.background1 是否会不时更改?因为我想知道为什么第二次我 运行 我的应用程序背景没有改变。

是的,您可以从 String

中获取 int id

试试这个

int background = getResources().getIdentifier("background", "drawable", "com.example.package");

image.setImageResource(background);