我正在尝试获取一个程序让用户导入自定义背景。
这是我所在的地方:
我的 getDrawable 函数将另一个函数作为参数:
mDrawableBg = getResources().getDrawable(getImage());
getImage() 假设返回一个引用所选图像的整数,这是该函数的代码(到目前为止):
public int getImage(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 10);
}
假设打开图库并让用户选择图像。然后我会使用 mDrawableBg 来设置背景。不过,我不确定如何返回所选图像的引用 ID。有什么建议吗?
最佳答案
试试这个:
String pathName = "selected Image path";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.container);
view.setBackgroundDrawable(bd);
关于java - 返回图像资源 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11643465/