【问题标题】:converting drawable resource image into bitmap将可绘制资源图像转换为位图
【发布时间】:2012-02-01 19:00:06
【问题描述】:

我正在尝试使用获取位图图像的Notification.Builder.setLargeIcon(bitmap)。我的可绘制文件夹中有我想要使用的图像,那么如何将其转换为位图?

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    您可能是指Notification.Builder.setLargeIcon(Bitmap),对吧? :)

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
    notBuilder.setLargeIcon(largeIcon);
    

    这是将资源图像转换为 Android Bitmaps 的好方法。

    【讨论】:

    • 为什么不点击“编辑”按钮解决问题? (更多关于未来的建议 - 我已经为这个做了......我建议编辑你的答案,不要批评他们的错别字。我不是为你做的。)另一方面,+1 有一个工作答案:)
    • 我不认为这是正确的作为一个普遍的答案 - 至少自从 Android 开始支持矢量绘图以来不是。
    • 在实施解决方案后我得到了这个...... E/CommitToConfigurationOperation: Malformed snapshot token (size): ... E/NotificationService: Not posting notification with icon==0: Notification(pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE) ... E/NotificationService: WARNING: In a future release this will crash the app:...
    【解决方案2】:
    Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
    Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
    

    由于 API 22 getResources().getDrawable() 已弃用,因此我们可以使用以下解决方案。

    Drawable vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.logo,  getContext().getTheme());
    Bitmap myLogo = ((BitmapDrawable) vectorDrawable).getBitmap();
    

    【讨论】:

    • 它告诉我 bitmapDrawable 无法解析为类型
    • 如果接收到的无法解析为 bitmapDrawable 的类型,只需按 ctrl+shift+O。干杯!
    • 不幸的是,这种方式让我的应用崩溃了!
    • getDrawable 已弃用
    【解决方案3】:
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
    

    Context 可以是您当前的Activity

    【讨论】:

    • 对于矢量绘图?
    【解决方案4】:

    这里是在android中将Drawable资源转换为Bitmap的另一种方法:

    Drawable drawable = getResources().getDrawable(R.drawable.input);
    Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
    

    【讨论】:

    • 您的解决方案与 AndyW 解决方案有何不同?是一样的!
    【解决方案5】:

    首先创建位图图像

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
    

    现在在通知生成器图标中设置位图......

    Notification.Builder.setLargeIcon(bmp);
    

    【讨论】:

      【解决方案6】:

      res/drawable 文件夹中,

      1.创建一个新的Drawable Resources

      2.输入文件名。

      将在res/drawable 文件夹中创建一个新文件。

      在新创建的文件中替换此代码,并将ic_action_back 替换为您的可绘制文件名。

      <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
          android:src="@drawable/ic_action_back"
          android:tint="@color/color_primary_text" />
      

      现在,您可以将其与资源 ID R.id.filename 一起使用。

      【讨论】:

        猜你喜欢
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        • 2019-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-31
        相关资源
        最近更新 更多