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

tyc*_*czj 168 android bitmap

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

poi*_*oae 396

你可能是的意思Notification.Builder.setLargeIcon(Bitmap),对吗?:)

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
notBuilder.setLargeIcon(largeIcon);
Run Code Online (Sandbox Code Playgroud)

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

  • 为什么不点击"编辑"按钮并修复问题?(更多关于未来的建议 - 我已经为此做了...我建议编辑你的答案,不要批评他们的错别字.我不是为你做的.)另一方面,+1有一个工作答案:) (2认同)

And*_*dyW 43

Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
Run Code Online (Sandbox Code Playgroud)

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

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


aro*_*ero 13

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
Run Code Online (Sandbox Code Playgroud)

Context可以是你当前的Activity.

  • 和矢量drawables? (2认同)

Ram*_*ash 9

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

Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Run Code Online (Sandbox Code Playgroud)

  • 你和AndyW解决方案有什么不同?这是相同的! (2认同)

Rav*_*ana 6

首先创建位图图像

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Run Code Online (Sandbox Code Playgroud)

现在在Notification Builder Icon中设置位图....

Notification.Builder.setLargeIcon(bmp);
Run Code Online (Sandbox Code Playgroud)