【问题标题】:Dynamically handling image resource动态处理图片资源
【发布时间】:2012-07-06 19:10:15
【问题描述】:

我想动态更改位图的图像资源。但我不能用可绘制类中的名称来调用它们。

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.getImage(foldername + "/" + imagename));

我需要做类似的事情,但我找不到正确的方法。,

已编辑

我想我应该更清楚。我将我的图像存储在 drawable 文件夹下,它们被分隔到其他文件夹中。

例如;

drawable/imageset1,drawable/imageset2,

我想根据用户输入更改位图的图像资源。

例如: 用户从第一个微调器中选择 imageset5,并从另一个微调器中选择 image5.png。

【问题讨论】:

  • @Srht,我准备了演示,它正在工作。它有点像重复的代码,但如果你想要它,那就在这里评论吧。
  • @ChintanRaghwani 它对我不起作用。是的,我想要工作代码。
  • 当您将图像存储在可绘制文件夹下时,我会工作。但它不适用于文件夹层次结构。我认为Android不支持drawable下的文件夹。
  • 我已经添加并更新了答案,请检查一下。

标签: android dynamic bitmap


【解决方案1】:

我希望这会做你想要的

BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(foldername + "/" + imagename , "drawable", getPackageName());

已编辑:

虽然上面的代码只有在你遵循不允许drawable文件夹中的子目录的android规则时才有效,所以上面的代码只有在直接从drawable访问图像时才有效。

BitmapFactory.decodeResource(getResources(), getResources().getIdentifier( imagename , "drawable", getPackageName());

正如这些链接所描述的

How to access res/drawable/"folder"

Can the Android drawable directory contain subdirectories?

【讨论】:

    【解决方案2】:

    制作一组图片资源

    int []a = int[]
    {
       R.drawable.one,
       R.drawable.two,
       R.drawable.three,
       R.drawable.four,
    };
    

    然后循环访问

    for(int i=0;i<a.length;i++)
    {
       //  a[i]
    }
    

    【讨论】:

    • 我有 500 多张图片被分成文件夹。
    【解决方案3】:

    它是存储在drawables文件夹中的资源吗?然后从 BitmapFactory 试试这个方法

    public static Bitmap decodeResource (Resources res, int id)
    

    在哪里

    res = getResources()
    

    id 是可绘制对象的 id

    R.drawable.picture
    

    看看here

    【讨论】:

      【解决方案4】:

      你不能这样做。你必须在drawable中添加所有图像并像使用它一样使用它

      myImage.setBackgroundResource(R.drawable.imageName)

      或从网上下载图片后,您可以申请为

      myImage.setBitmap(BitmapFactory.decodeStream(in));
      

      获取InputStream的位图

      【讨论】:

        【解决方案5】:

        只需尝试以下操作: 您的名为“imagename”的图片必须在文件夹中

        String IMAGE_FOLDER_NAME = "YOUR_IMAGE_CONTAINING_FOLDER";
        

        现在,使用以下代码:

        String imagename = "YOUR_IMAGE_NAME";    
        String PACKAGE_NAME=getApplicationContext().getPackageName();    
        int imgId = getResources().getIdentifier(PACKAGE_NAME+":"+IMAGE_FOLDER_NAME+"/"+imagename , null, null);
        System.out.println("IMG ID :: "+imgId);    //    check this in log
        System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);    //    check this in log
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
        

        【讨论】:

        • @Srht,如果此代码不起作用并且日志显示错误,请在此处发布该错误。
        • 我在 res 下创建了一个名为“teamimages”的文件夹,但它不允许我使用该文件夹进行编译。
        • 我什至无法编译。它说您的项目包含错误。并将包资源管理器中的图像文件夹显示为错误。
        • 我认为android也不允许我们随心所欲地创建文件夹。
        • 等等,我通过在我的项目中添加“teamimages”来检查它
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-26
        • 1970-01-01
        • 1970-01-01
        • 2018-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多