How to convert a Bitmap to Drawable in android?
Sounds like you want to use BitmapDrawable
From the documentation:
A
Drawable
that wraps a bitmap and can
be tiled, stretched, or aligned. You
can create aBitmapDrawable
from a
file path, an input stream, through
XML inflation, or from aBitmap
object.
How to convert a Drawable to a Bitmap?
This converts a BitmapDrawable to a Bitmap.
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
How to create Bitmap form Drawable object
You can convert your Drawable
to Bitmap
like this (for resource):
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.drawable_source);
OR
If you've it stored in a variable, you can use this :
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
More details
converting drawable resource image into bitmap
You probably mean Notification.Builder.setLargeIcon(Bitmap)
, right? :)
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
notBuilder.setLargeIcon(largeIcon);
This is a great method of converting resource images into Android Bitmap
s.
How to load an image from drawable and convert to a bitmap
The following method should work fine:
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
build.gradle (project-level)
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha5'
}
build.gradle (app-level)
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
vectorDrawables.useSupportLibrary = true
}
...
}
...
Or, you can use android-ktx
in Kotlin too like this which works for any subclass of Drawable
:
build.gradle
dependencies {
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
}
Then use Drawable#toBitmap()
like this:
val bitmap = AppCompatResources.getDrawable(requireContext(), drawableId).toBitmap()
Related Topics
How to Send and Receive Data Sms Messages
What Is the Real Android Studio Gradle Version
Get the Current Fragment Object
Kill Another Application on Android
How to Implement Filterable in Realmrecyclerviewadapter
Android Studio - Unable to Merge from Svn Branch
Unresolved Reference - Activity Does Not Recognize Synthetic Imports in Android Studio V4
How to Use Icons and Symbols from "Font Awesome" on Native Android Application
Android Viewpager with Bottom Dots
How Does the Mapping Between Android Resources and Resources Id Work
Add Ripple Effect to My Button with Button Background Color
Difference Between Activity and Fragmentactivity
Changing Position of the Dialog on Screen Android
Detect the Status of Two Sim Cards in a Dual-Sim Android Phone