How to convert imageurl to drawable? - android

First of all i searched a lot and didn't find solution for my case, i need to convert the imageUrl to drawable, what i tried as far :
File imageFile = DiskCacheUtils.findInCache(c.profilePhoto, com.nostra13.universalimageloader.core.ImageLoader.getInstance().getDiscCache());
String file_target = "file://" + imageFile.getPath();
BitmapDrawable bitDraw = (BitmapDrawable) BitmapDrawable.createFromPath(file_target);
if (profilePhotos.size() == 4) break;
BitmapDrawable drawable = bitDraw; //getResources().getDrawable(c.profilePhoto);
drawable.setBounds(0, 0, width, height);
profilePhotos.add(drawable);
Also i tried to load the image into bitmap via Glide, but didn't work.
How i can achieve that ?
Thanks in advance.

first convert URL to Bitmap and pass bitmap into these function
Drawable d= new BitmapDrawable(context.getResources(), bitmap);
return d;

Related

Failed to decode image. The provided image must be a Bitmap

BitmapDescriptor bmpD = BitmapDescriptorFactory.fromResource(R.raw.podval);
Log.d("myLog", ":" + bmpD);
GroundOverlayOptions newarkMap = new GroundOverlayOptions()
.image(bmpD)
.position(sydney, 8600f, 6500f);
Log.d("myLog", ":" + newarkMap);
GroundOverlay imageOverlay = mMap.addGroundOverlay(newarkMap);
Failed to decode image. The provided image must be a Bitmap.
But in log I got :com.google.android.gms.maps.model.BitmapDescriptor#58e0ee6
:com.google.android.gms.maps.model.GroundOverlayOptions#1ea9c27
Also I converted jpg image to bmp, after failing to convert it here
Help please.
See https://stackoverflow.com/a/45564994/2914140 for vector drawables (SVG).
private BitmapDescriptor bitmapDescriptorFromVector(Context context, #DrawableRes int vectorResId) {
Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
Place your image in the drawable folder, not in the raw folder. Also make sure the resource is a valid image file.

How to get bitmap from NetworkImageView?

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
}

Create drawable from bitmap

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

Set photo as marker in map

Let's say I got the absolute path into the photo. /mnt/sdcard/....jpg
String path = "/mnt/sdcard/....jpg";
BitmapDrawable d = new BitmapDrawable(this.getResources(), path);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
overlayItem.setMarker(d);
This is what i tried, however the photo does not display.
EDIT: fixed, wrong path to file.
Bitmap b = BitmapFactory.decodeFile(path);
Ok, and what about this:
Drawable d = Drawable.createFromPath(path);
Can you post your code?
The way Pasha said should work.
Let's say you have an image in you drawable folder named your_image, you could do like this:
Point point = new Point();
yourMapView.getProjection().toPixels(yourGeoPoint, point);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.your_image);
yourCanvas.drawBitmap(bmp, point.x, point.y-(your_size), null);

How to convert a Bitmap to Drawable in android?

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

Categories

Resources