【问题标题】:Array with images from drawable and intent包含来自可绘制和意图的图像的数组
【发布时间】:2016-01-26 22:25:34
【问题描述】:

我有一张图片列表:

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)? 还是有第三种完全不同的更好的解决方案?

【问题讨论】:

  • @Evin1_ 你有解决办法还是只是纠正缩进?
  • 我不打算回答,但你去吧!让我知道它是否有效。

标签: java android image android-intent


【解决方案1】:

如果您使用 API 17 或更高版本,则可以使用 View.generateViewId() 生成 ID。

来自主要文档:

生成一个适合在 setId(int) 中使用的值。这个值不会 与 aapt for R.id 在构建时生成的 ID 值发生冲突。

返回一个生成的 ID 值

您可以查看此答案以查看使用较低 API 时的替代方案:Android: View.setID(int id) programmatically - how to avoid ID conflicts?

【讨论】:

  • 在这种情况下视图会是什么?此时我只有一个 URI,我可以从中创建的唯一视图类型是 ImageView。 ImageView的ID可以正确设置并存储在列表中,但是当我用ID调用“setImageResource(int id)”时,什么都没有显示。
【解决方案2】:

我认为您正在尝试做的事情有些误导。 /res 目录中的资源是与项目捆绑在一起的编译时资源。一个人通过他们的设备中的意图选择的图像是一个运行时文件,该文件会因用户、设备等而异。你最好不要试图对它们进行相同的处理。将用户选择保存为 dataUris 或字符串,并将资源保存为整数。

【讨论】:

    猜你喜欢
    • 2011-03-23
    • 2017-07-09
    • 1970-01-01
    • 2017-10-22
    • 2013-11-12
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多