I am trying to draw an image from a file, a png file.
I need to draw it as a bitmap on a canvas but i cant get the file into the bitmap object without getting an error, after trying for a lot of time i need some help.
example of what i tried:
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.myimage);
thanks!
Related
I have a Bitmap and i want to configure it. I also set my bitmap to Mutable one that's not a problem but when i want to configure, it comes with a error that my bitmap is not large enough.
Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.ic_cm);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888,true);
bitmap.setWidth(getWidth() / 2);
In Application i need to load a few Bitmaps on create.
To save Memory i'm loading one image, rescale it by creating a scaledBitmap out of it, recycle the unscaled Bitmap on so on:
bmpUnscaled = BitmapFactory.decodeResource(getResources(), R.drawable.cultivation_plant_resized_1);
plant1 = Bitmap.createScaledBitmap(bmpUnscaled,(int) plant[0].getWidth(), (int) plant[0].getHeight(), true);
bmpUnscaled.recycle();
System.gc();
bmpUnscaled = BitmapFactory.decodeResource(getResources(), R.drawable.cultivation_plant_resized_2);
plant2 = Bitmap.createScaledBitmap(bmpUnscaled,(int) plant[0].getWidth(), (int) plant[0].getHeight(), true);
bmpUnscaled.recycle();
System.gc();
and so on...
I am doing this 10 Images, wich are scaled relative to the screen resolution.
The orginal Image is a PNG (570x900 (max. 660KB))
Does anybody have some Ideas to save Memory simply?
I'm desperate right now...
Have you read: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html?
The article contain great information about working with BitMap.
I've created a bitmap from canvas. Save it to my "/sdcard/folder/subfolder/file.png"
I want to get this png file into imageview after saving it. I tried this by using BitmapFactory.decodeFile("/sdcard/folder/subfolder/file.png"); method. But it returned nothing. There is no image on imageview.
Do you have any idea?
Try using
String filePath = Environment.getExternalStorageDirectory()+"/folder/subfolder/file.png";//or "folder/subfolder.."
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)
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/