Android convert ImageIcon to Drawable - android

I'm making an app that take datas from a database and it should take an ImageIcon. How can I use that class in Android and convert it to Drawable to show it in app?
It's not from a resource file. It's from an ImageIcon as variable of an object i receive from database.

try this
Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.png");
Drawable d = new BitmapDrawable(getResources(), bitmap);

Related

Get Bitmap from drawable resource, Android

I use this code to get a Bitmap object from a bitmap image from drawable resources:
Bitmap avatar = ((BitmapDrawable) getDrawable(R.drawable.avatar_bpm)).getBitmap();
The code works but android studio suggests I use: Use ContextCompat.getDrawable()
What is it mean? And how do I convert my code to use the suggested version ?

Android BitmapDrawable load from url

If I have the URL String such as str_URL = "http://........ABC.png";
How can I add it to below which replace R.drawable.jth?
BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.jth);
BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.jthj);
Android doesn't allow to set an URL as a drawable's source directly, you'll need to write your code to download the file and then use a bitmapfactory to create your drawable.
If you don't want to write code to do that, you can use some open source libraries like Koush's URLImageViewer, if what you want is to display your drawable on an ImageView

converting drawable resource image into bitmap

I am trying to use the Notification.Builder.setLargeIcon(bitmap) that takes a bitmap image. I have the image I want to use in my drawable folder so how do I convert that to 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 Bitmaps.
Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
Since API 22 getResources().getDrawable() is deprecated, so we can use following solution.
Drawable vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.logo, getContext().getTheme());
Bitmap myLogo = ((BitmapDrawable) vectorDrawable).getBitmap();
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
Context can be your current Activity.
Here is another way to convert Drawable resource into Bitmap in android:
Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
First Create Bitmap Image
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
now set bitmap in Notification Builder Icon....
Notification.Builder.setLargeIcon(bmp);
In res/drawable folder,
1. Create a new Drawable Resources.
2. Input file name.
A new file will be created inside the res/drawable folder.
Replace this code inside the newly created file and replace ic_action_back with your drawable file name.
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_action_back"
android:tint="#color/color_primary_text" />
Now, you can use it with Resource ID, R.id.filename.
If someone is looking for the Kotlin version for the large icon, you may use this
val largeIcon = BitmapFactory.decodeResource(context.resources, R.drawable.my_large_icon)

Working with canvas

I am able to draw a signature on my canvas. Now after that I want to save this signature as an image which can be used later.
I am unable to implement an onClicklistener on this canvas. Also I am unclear as to how this signature can be stored as an image. Please suggest?
Try this:
Bitmap bmp = Bitmap.createBitmap(...);
Canvas can = new Canvas(bmp);
When you change your canvas, bitmap bmp will change too. Canvas is only raference to Bitmap canvas, and you have no need to save canvas. Save only Bitmap (bmp).
To save the canvas drawing as an image, you need to convert it to a data url using the toDataURL method. Once you have the data url, you can use it to set the source of an image element so the user can right click and download the image.:
// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();
// set canvasImg image src to dataURL
// so it can be saved as an image
document.getElementById("canvasImg").src = dataURL;
Reference: http://www.html5canvastutorials.com/advanced/html5-canvas-save-drawing-as-an-image/

Is it possible to retrieve an image from the data folder in an android app?

I want to retrieve an image from my data/data/com.apps.myapp/images folder and display it in an ImageView. Any clue?
Try this :
Bitmap bitmap = BitmapFactory.decodeFile("data/data/com.apps.myapp/images/img.png");
ImageView imgView = (ImageView) this.findViewById(R.id.imgViewId);
imgView.setImageBitmap(bitmap);
There are several components involved in this.
To get the path of your data folder you can use the method getDir in the Context.
Now you have to know the file name and open an stream here again the Context class is your friend. Now the stream can be decoded into a Bitmap via a Bitmap Factory.
After you got a Bitmap create a BitmapDrawable from it and pass it to your ImageView

Categories

Resources