Getting Bitmap from vector drawable
Checked on API: 17, 21, 23
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;
}
UPDATE:
Project gradle:
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha5'
}
Module gradle:
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
vectorDrawables.useSupportLibrary = true
}
...
}
...
How to get a Bitmap from VectorDrawable
First of all, you cannot cast VectorDrawable
to BitmapDrawable
. They don't have a parent-child relationship. They both are direct subclasses of Drawable
class.
Now, to get a bitmap from drawable, you will need to create a Bitmap
from the drawable metadata.
Probably something like this in a separate method,
try {
Bitmap 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;
} catch (OutOfMemoryError e) {
// Handle the error
return null;
}
I hope this helps.
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 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()
VectorDrawable to Bitmap: Bitmap.setHasAlpha(boolean)' on a null object reference
change
android:width="16000dp"
android:height="16000dp"
to
android:width="24dp"
android:height="24dp"
don't need to chagne:
android:viewportWidth="16000"
android:viewportHeight="16000">
Decoding bitmap from Drawable Resource Id is giving null
Please check if you are using Vector drawable or normal png/jpeg image as a drawable, it gives null value if you try to decode vector drawable.
Use this code if you are trying to decode Vector drawable
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;
}
Also make sure if you have vector support enable in your app gradle
vectorDrawables.useSupportLibrary = true
Related Topics
How to Reference Style Attributes from a Drawable
Open an Image Using Uri in Android's Default Gallery Image Viewer
How to Pass Integer from One Activity to Another
How to Add a Dropdown Item on the Action Bar
Android - How to Unregister a Receiver Created in the Manifest
Android - How to Set the Wallpaper Image
Is There a List of Screen Resolutions for All Android Based Phones and Tablets
Prevent Dialog Dismissal on Screen Rotation in Android
Unsupported Major.Minor Version 52.0 When Rendering in Android Studio
Android: How to Detect Double-Tap
Customize Android Facebook Login Button
Manifest Merger Fails for Appcomponentfactory
Phonegap - Add a Splash Screen for Android App
How to Check If an Activity Is the Last One in the Activity Stack for an Application
Using Gson to Parse a JSON with Dynamic "Key" and "Value" in Android
Resolved Versions for App (22.0.0) and Test App (21.0.3) Differ