【问题标题】:Convert Drawable/Bitmap to ResourceID (android)将 Drawable/Bitmap 转换为 ResourceID (android)
【发布时间】:2013-02-01 01:19:34
【问题描述】:

所以我需要有一个基于我 SDCARD 上的 Image 的图标,但我只能获取 Drawable/Bitmap,我怎样才能基于它获取 int 资源?

public static String SDCARD_PATH = Environment.getExternalStorageDirectory().getPath() + "/folder/";

public static Drawable getDrawableFromChannelID(int id) {
    return Drawable.createFromPath(Utils.SDCARD_PATH + id + ".png");
}

public static Bitmap getBitmapFromChannelID(int id) {
    return BitmapFactory.decodeFile(Utils.SDCARD_PATH + id + ".png");
}

【问题讨论】:

  • 你最后做了什么?
  • 嘿,看起来不可能:/ 看看@shesmile 的回答,祝你好运

标签: android resources bitmap drawable


【解决方案1】:

是的,我相信你不能那样做。您必须在资源中手动添加图像以获取 id,或者您可以将其添加为 Contentview 中的 ImageView 并在其上添加 Id,只需将其可见性设置为 Gone,这样就不会影响您的其他布局,像这样

   ImageView icon=new ImageView(this);
   img.setBackground(<urDrawableIcon>);
   RelativeLayout.LayoutParams iconlayout=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
   icon.setId(11111);//set your own id;
   icon.setVisibility(View.GONE);
   addContentView(icon,iconlayout);

   //so your id is 11111; NotificationBuilder.setIcon(11111);

【讨论】:

  • NotificationBuilder.setIcon(res) 我需要它,图标只在第一次运行后下载,我喜欢你的方法
【解决方案2】:

我认为你不能。所有 R.id.. 等都是资源included in your apk。为什么你仍然需要一个资源ID?

【讨论】:

  • NotificationBuilder.setIcon(res)需要它,图标是第一次运行后才下载的