简体   繁体   English

包含来自可绘制和意图的图像的数组

[英]Array with images from drawable and intent

I have a list of images: 我有一张图片清单:

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

I want to let the user pick an image from the gallery by using an Intent: 我想让用户使用Intent从图库中选择图像:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

One of the images of the array should then be replaced with the new image given by the user. 然后应将阵列的图像之一替换为用户提供的新图像。 When I get the image from the user, I only have a URI of the image: 当我从用户那里获得图像时,我只有图像的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
    }
}

Is it possible to somehow get an integer ID of the image so that I can insert the image into the same list as the images from the drawable folder? 是否有可能以某种方式获取图像的整数ID,以便将图像插入到与来自drawable文件夹的图像相同的列表中? Or should I instead try to store a list of URI's (if it is possible to get URI's of the images from the drawable folder)? 还是我应该尝试存储URI的列表(如果可以从drawable文件夹中获取图像的URI)? Or is there a third solution that is completely different and much better? 还是存在完全不同且更好的第三种解决方案?

You can generate ids with View.generateViewId() if you are using API 17 or larger. 如果您使用的是API 17或更高版本,则可以使用View.generateViewId()生成ID。

From the main documentation: 从主要文档中:

Generate a value suitable for use in setId(int). 生成适合在setId(int)中使用的值。 This value will not collide with ID values generated at build time by aapt for R.id. 该值不会与Aapt for R.id在生成时生成的ID值冲突。

Returns a generated ID value 返回生成的ID值

You can check this answer to see what are the alternatives when you are using a lower API: Android: View.setID(int id) programmatically - how to avoid ID conflicts? 您可以检查以下答案,以查看使用较低API时的替代方法: Android:以编程方式查看-View.setID(int id)-如何避免ID冲突?

I think what you are trying to do is somewhat misguided. 我认为您尝试执行的操作有些误导。 The resources in the /res directory are compile time resources that you bundle with your project. / res目录中的资源是与项目捆绑在一起的编译时资源。 The image that a person selects via an intent from their device is a run time file that will vary by user, device, etc. You are better off not trying to treat them the same. 人们通过意图从他们的设备中选择的图像是运行时文件,该文件会因用户,设备等的不同而有所差异。最好不要尝试对它们进行相同的处理。 Save the user selections as dataUris or strings and keep the resources as ints. 将用户选择另存为dataUris或字符串,并将资源保留为int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM