我有一个图像列表:
private int[] images = {
R.drawable.blue_icon_left_foot,
R.drawable.blue_icon_right_foot,
R.drawable.blue_icon_left_hand,
R.drawable.blue_icon_right_hand,
R.drawable.green_icon_left_foot,
R.drawable.green_icon_right_foot,
R.drawable.green_icon_left_hand,
R.drawable.green_icon_right_hand,
R.drawable.red_icon_left_foot,
R.drawable.red_icon_right_foot,
R.drawable.red_icon_left_hand,
R.drawable.red_icon_right_hand,
R.drawable.yellow_icon_left_foot,
R.drawable.yellow_icon_right_foot,
R.drawable.yellow_icon_left_hand,
R.drawable.yellow_icon_right_hand};
我想让用户使用 Intent 从图库中选择图像:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
然后应该用用户提供的新图像替换数组中的图像之一。当我从用户那里获取图像时,我只有图像的 URI:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
Uri imageUri = data.getData();
//Insert image into images list
}
}
是否可以以某种方式获取图像的整数 ID,以便我可以将图像插入与可绘制文件夹中的图像相同的列表中? 或者我应该尝试存储 URI 列表(如果可以从可绘制文件夹中获取图像的 URI)? 或者是否有第三种完全不同且更好的解决方案?
最佳答案
您可以使用View.generateViewId()生成ID如果您使用的是 API 17 或更高版本。
来自主要文档:
Generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.
Returns a generated ID value
您可以检查此答案,看看当您使用较低的 API 时有哪些替代方案:Android: View.setID(int id) programmatically - how to avoid ID conflicts?
关于java - 包含来自可绘制图像和 Intent 的图像的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35025227/