ALL,
As suggested here I need to make a drawable out of my bitmap. But when I tried to use:
Drawable d = new Drawable( my_bmp);
it shows that this constructor is deprecated in favour of:
Drawable(Bitmap bmp, int resourceId)
How else I can make a drawable out of bitmap?
Thank you.
You can use
Drawable d = new BitmapDrawable(getResources(), my_bmp);
A Drawable that wraps a bitmap and can be tiled, stretched, or
aligned. You can create a BitmapDrawable from a file path, an input
stream, through XML inflation, or from a Bitmap object.
BitmapDrawable(Resources res, Bitmap bitmap)
Create drawable from a bitmap, setting initial target density based on
the display metrics of the resources.
Also look a the public constructors #
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
try this to load Bitmap as Bitmap Drawable
Create ImgDrawableFromFile(Resources res, String file_name) like below
Drawable d = null;
public Drawable ImgDrawableFromFile(Resources res, String file_name) {
myBitmap = null;
File imgFile = new File(
"/data/data/yourpkgname/app_my_sub_dir/images/" + file_name);
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if (myBitmap != null) {
d = new BitmapDrawable(res, myBitmap);
return d;
} else {
return null;
}
}
return null;
}
Now called this function like
img.setBackgroundDrawable(ImgDrawableFromFile(getResources(), "1.jpg")); // pass your saved file name as second argument
A little late to the party but I see a clear misunderstanding. The answers provided are indeed correct:
Drawable d = new BitmapDrawable(getResources(),bitmap);
In this case getResources() is a method available in the Context or Activity and is only used to find out the screen density and adjust the Drawable accordingly.
So Igor, even if you are loading the image from the cloud, you can still call getResources().
Happy coding
BitmapDrawable(Bitmap bitmap) constructor has been deprecated
you can use
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
source
you can use the BitmapDrawable class to get a Drawable from a bitmap
Bitmap Drawable reference
If Mauro Banze was late to the party then I don't really know what I'm still doing here. But for the ones that come here desperately looking for a solution, here is what I did:
I stumbled over somewhat the same issue when bringing a Project from JavaFX to Android. I wanted to reuse most of my "data" classes which only manage and provide my data and have no affect on the UI.
Enough story telling, here we go:
Problem:
At some point we need a Drawableto get out Bitmap on the screen.
We want to download (from the cloud, the internet, or wherever) this Bitmap and save it for later usage as a Drawable
We can't do that in a non-Context or non-Activity class, as we need the Resources.
So why don't we just save it as a Bitmap until we need it to be drawn (which certainly will happen in a Context or Activity class).
I have created a class called BitmapImage. It looks like this:
public class BitmapImage {
private final Bitmap bitmap;
public BitmapImage (Bitmap bitmap) {
this.bitmap = bitmap;
}
public Bitmap getBitmap() {
return bitmap;
}
public Drawable getDrawable(Resources res) {
return new BitmapDrawable(res,getBitmap());
}
This very simple class "saves" the Bitmap. Thus, rather then working with a Drawable you work with the BitmapImage until you really desperately need the Drawable.
At that point, you should be in your Activity or Context and there you can call Drawable foo = anyBitmapImage.getDrawable(getResource()).
Related
In my project I used (Volley + NetworkImageView) to download some images and texts and showing them in a list view.. till here, I don't have any problem.
Now, I want to get bitmaps form NetworkImageView and I tried many methods like the following, but non of them worked for me.
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Another method:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
Non of them worked..
Any Help is appreciated,,
You cannot get the Bitmap reference as it is never saved in the ImageView.
however you can get it using :
((BitmapDrawable)this.getDrawable()).getBitmap();
beacuse when you set it with Volley u do this:
/**
* Sets a Bitmap as the content of this ImageView.
*
* #param bm The bitmap to set
*/
#android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
however if you set your default image or error image or any other image in any other way you may not get BitmapDrawable but NinePatchDrawable for example.
here is how to check:
Drawable dd = image.getDrawable();
if(BitmapDrawable.class.isAssignableFrom(dd.getClass())) {
//good one
Bitmap bb = ((BitmapDrawable)dd).getBitmap();
} else {
//cannot get that one
}
I'm facing problem in adding a drawable object to a Parcel object, in order to send an object from one activity to another activity.
There are methods like writeString(String) to add strings to a Parcel object. But do not know how to add a Drawable to the Parcel object.
You cannot add a Drawable to a Parcel, as Drawable does not implement the Parcelable interface. Certain types of Drawable might implement Parcelable, but I am not aware of any.
You can put in the Parcel some identifier (e.g., drawable resource ID) and have the recipient obtain the Drawable on its own.
I think you can try wrapping the Drawable inside an object that implements Parcelable.
You can do that using Bitmap and BitmapDrawable.
Let's say you have a Drawable called drawable and a Parcel (in which the object should be written) called parcel.
To write a Drawable into a Parceable:
if ( drawable != null ) {
Bitmap bitmap = (Bitmap) ((BitmapDrawable) drawable).getBitmap();
parcel.writeParcelable(bitmap, flags);
}
else {
parcel.writeParcelable(null, flags);
}
To read the Drawable from the Parceable:
Bitmap bitmap = (Bitmap) in.readParcelable(getClass().getClassLoader());
if ( bitmap != null ) {
drawable = new BitmapDrawable(bitmap);
}
else {
drawable = null;
}
In particular, since the constructor BitmapDrawable (Bitmap bitmap) has been deprecated, you might want to use BitmapDrawable (Resources res, Bitmap bitmap) instead.
drawable = new BitmapDrawable(context.getResources(), bitmap);
Perhaps very simple, but not for me ;)
How do i change the Picture of a Framelayout in my sourcecode?
My first intention was:
mBackgroundDrawable = getResources().getDrawable(R.drawable.picture);
main.setBackgroundDrawable(mBackgroundDrawable);
but there i only can add pictures from my "RES/DRAWABLE-HDPI" folder.
I want to use my SD-Card as the destinated source.
Whats the right way?
Try this:
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);
BitmapDrawable dr=new BitmapDrawable(bMap);
You'll need to convert your Bitmap to a BitmapDrawable before able to set it as a background.
val bitmap = BitmapFactory.decodeFile(this.file.absolutePath)
frameLayout.background = BitmapDrawable(context.resources, bitmap)
I am loading a bitmap from a resource like so:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
What I want to do is make some changes to the bitmap before It gets drawn to the main canvas in my draw method (As it would seem wasteful to repeat lots of drawing in my main loop when it isn't going to change). I am making the changes to the bitmap with the following:
Canvas c = new Canvas(mBackground);
c.drawARGB(...); // etc
So naturally I get an exception
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
So to avoid that I made a copy of the bitmap so that it is mutable
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image).copy(Bitmap.Config.ARGB_8888, true);
Which avoid the problem however it sometimes causes OutOfMemoryExceptions, do know any better ways of achieving what I want?
Use decodeResource(Resources res, int id, BitmapFactory.Options opts) and specify inMutable in the options.
http://developer.android.com/reference/android/graphics/BitmapFactory.html
You'd better use RapidDecoder.
import rapid.decoder.BitmapDecoder;
Bitmap mBackground = BitmapDecoder.from(res, R.drawable.image)
.mutable().decode();
Works for API level 8.
Instad of yours:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
Use:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image, options);
How can I convert a Bitmap image to Drawable ?
Try this it converts a Bitmap type image to Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
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 a BitmapDrawable from a
file path, an input stream, through
XML inflation, or from a Bitmap
object.
Having seen a large amount of issues with bitmaps incorrectly scaling when converted to a BitmapDrawable, the general way to convert should be:
Drawable d = new BitmapDrawable(getResources(), bitmap);
Without the Resources reference, the bitmap may not render properly, even when scaled correctly. There are numerous questions on here which would be solved simply by using this method rather than a straight call with only the bitmap argument.
Offical Bitmapdrawable documentation
This is sample on how to convert bitmap to drawable
Bitmap bitmap;
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
I used with context
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
1) bitmap to Drawable :
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
2) drawable to Bitmap :
Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
// mImageView.setImageBitmap(mIcon);
If you have a bitmap image and you want to use it in drawable, like
Bitmap contact_pic; //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic);
Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}
here's another one:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
For Kotlin users:
Kotlin has a function for converting Bitmap to BitmapDrawable:
Bitmap.toDrawable(resources)
covert bit map to drawable in sketchware app using code
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);