【问题标题】:How to get asset images id in android如何在android中获取资产图像ID
【发布时间】:2014-01-25 19:22:59
【问题描述】:

我需要在 ViewPager 中设置图像列表。我的 asset 文件夹中有一个图像列表。我将图像分组在 asset 内的单独文件夹中,例如 assets/images; assets/images1,...... 现在我需要在 int[] 数组中获取图像的 id(例如 images1 单独)。 我在 String[] 数组中获得了图像(images1)的名称。如何使用它们的名称获取字符串 [] 中图像的 id。

【问题讨论】:

  • 有什么强制的,你要从asset文件夹中获取数据
  • 你必须从asset获取位图并放到imageview上

标签: android


【解决方案1】:

您必须将图像放在可绘制文件夹中才能访问它们。然后您可以使用此代码从图像的名称中获取图像的 id..

 String yourImageName;
 int resID = getResources().getIdentifier(yourImageName, "drawable", "com.example.yourpackegename.");

【讨论】:

    【解决方案2】:

    在项目的“res/”中创建一个名为“drawable”的文件夹。

    假设你有像...这样的图像

    res/drawable/image,image1,image2,...

    在您的 ACTIVITY 中,使用 R.drawable.image,R.drawable.image1,.... 调用它们。

    "Now I need to get the id of the images (say images1 alone) in int[] array"
    

    您也可以将它们存储在数组中,也可以使用“R.drawable.YOUR_IMAGE_NAME”作为调用它们,

    int[] images = {R.drawable.image,R.drawable.image1,R.drawable.image2,R.drawable.image3,....};
    

    希望这会有所帮助。

    【讨论】:

    • 对不起,先生,我的文件夹中有近 100 张图片。
    • 好的,所以您可以在需要时使用“R.drawable.image_name”直接调用它们,对吗?如果您还需要使用数组调用它们,则必须在数组中调用“多次”!
    • 您使用资产文件夹的确切意图是什么。要清楚(据我所知),ANDROID 目录结构中的 Assets 文件夹用于存储一些静态文件,例如 JSON、Text Files 等,我们可以使用 InputStreams 来完成,例如“getAssets().open(”NEW_FILE 。文本”);” ...
    • 也检查此链接,get array of ID image when have array of name image in assets folder 在这里您可以通过列表引用您的图像名称。
    【解决方案3】:
    int[] array_images = {
                          R.drawable.image0,
                          R.drawable.image1,
                          R.drawable.image2,
                          R.drawable.image3,
                          R.drawable........,
                          ....
                          };
    

    【讨论】:

      【解决方案4】:

      通常只为资源文件夹中的图像创建“id”,而不为资产文件夹中的图像创建。

      【讨论】:

        【解决方案5】:

        如果您将图像保存在资产中,则必须使用资产管理器对其进行解码。

        公共类 MainActivity 扩展 Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
        }
        
        private void init() {
            InputStream is = null;
            ImageView imageView = new ImageView(this);
            ((RelativeLayout)findViewById(R.id.parent)).addView(imageView);
            try {
             is = getAssets().open("ic_launcher.png");//name of image in your asset folder
            } catch (IOException e) {
                e.printStackTrace();
            }
        
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            imageView.setImageBitmap(bitmap);
        
                 // if you dont want bitmap  you can use drawable
        
               // Drawable d = Drawable.createFromStream(is, null);
            //imageView.setImageDrawable(d);
        
        }   
        

        }

        否则将图像保存在 res 中的 drawable 文件夹中,并使用 R.drawable.imagename 使用它

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-10
          • 2021-06-10
          • 2011-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多