Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将可绘制资源图像转换为位图_Android_Bitmap - Fatal编程技术网

Android 将可绘制资源图像转换为位图

Android 将可绘制资源图像转换为位图,android,bitmap,Android,Bitmap,我正在尝试使用获取位图图像的Notification.Builder.setLargeIcon(位图)。我想在我的可绘制文件夹中使用图像,那么如何将其转换为位图 Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable); 上下文可以是您当前的活动您的意思可能是通知.Builder.setLargeIcon(位图),对吗?:) 这是一种将资源图像转换为AndroidBi

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

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);

上下文
可以是您当前的
活动

您的意思可能是
通知.Builder.setLargeIcon(位图)
,对吗?:)

这是一种将资源图像转换为Android
Bitmap
s的好方法

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();

以下是在android中将可绘制资源转换为位图的另一种方法:

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

首先创建位图图像

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
现在在通知生成器图标中设置位图

Notification.Builder.setLargeIcon(bmp);

res/drawable
文件夹中

1.创建一个新的
可绘制资源

2.输入文件名

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_action_back"
    android:tint="@color/color_primary_text" />
将在
res/drawable
文件夹中创建一个新文件

在新创建的文件中替换此代码,并用您的可绘制文件名替换
ic\u action\u 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

为什么不点击“编辑”按钮来解决这个问题?(更多的是对未来的建议——我已经为这一次做了……我建议编辑你的答案,不要批评他们的打字错误。我不是为你做的。)另一方面,+1有一个有效的答案:)我不认为这是一个正确的一般答案-至少不是因为Android开始支持矢量绘图。实施解决方案后,我得到了这个<代码>。。。E/CommitterConfigurationOperation:格式错误的快照令牌(大小):。。。E/NotificationService:不使用图标==0发布通知:通知(pri=0 contentView=null Vibration=null sound=content://settings/system/notification_sound 默认值=0x0标志=0x10颜色=0x00000000可视信息=PRIVATE)。。。E/NotificationService:警告:在未来的版本中,这将使应用程序崩溃:…它告诉我bitmapDrawable无法解析为类型Hi@20美分您是否尝试过如果接收到无法解析为bitmapDrawable类型,请按ctrl+shift+O。干杯不幸的是,这样会使我的应用程序崩溃!getDrawable已被弃用您的解决方案与AndyW解决方案有何不同?是一样的!矢量绘图机呢?