How to Save the drawing canvas in android? - android

I am using this API demo of the Developer site, THIS DEMO.
But i am wonder that how to save that image in to My Andrtoid Device.
Is please anyone give the Code to save that drawn image to the Android Device.
Thanks.

try this code
View content = your_view;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = content.getDrawingCache();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(path+"/image.png");
FileOutputStream ostream;
try {
file.createNewFile();
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.flush();
ostream.close();
Toast.makeText(getApplicationContext(), "image saved", 5000).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "error", 5000).show();
}

drawView.setDrawingCacheEnabled(true);
Bitmap bm = null;
drawView.destroyDrawingCache();
bm=drawView.getDrawingCache();
Then write the bitmap to file using bitmap factory.

One option is create another Canvas (as shown below) and repeat all your drawing on this new canvas.
Once done, call drawBitmap.
Bitmap bitmap = new Bitmap(// Set the params you like //);
Canvas canvas = new Canvas(bitmap);
// Do all your drawings here
canvas.drawBitmap(// The first picture //);
The best would be if there was a way to copy an existing canvas and then you wont need to re-draw everything but I couldn't find one.

I have implemented the below approach & worked for me.
Get your CustomView by using its id from xml file but not by instantiating the Customview.
View v = findViewById(R.id.custom_view);
//don't get customview by this way, View v = new CustomView(this);
int canvasWidth = v.getWidth();
int canvasHeight = v.getHeight();
Bitmap bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
ImageView imageView = findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
All code should be inside saveButton click listener.

Related

Android studio (Chipmunk) screen capture image gets darken

I am programming a simple drawing application with drawLines:
public void onDraw(Canvas canvas) {
setBackgroundColor(cfgBackground); //background
//canvas.drawColor(ConfigActivity.getCfgBackground(ConfigActivity.cfgBackground)); //to keep background when export to PNG/JPG
//title
paint.setColor(0xbb020a0d);
paint.setTextSize(40);
canvas.drawText(Title, 0, 40, paint);
paint.setColor(color);
paint.setStrokeWidth(thickness);
canvas.drawLines(lines, paint);
}
This is no problem. When export the screen to image file, the image shows normal in the preview screen (with all other photos in the phone), but when open this image, it shows darken, as following:
I have tried Method A/B/C like following, but the result not much different:
File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(sdCard, imageName);
FileOutputStream fos = null;
try {
// Make sure the Pictures directory exists.
sdCard.mkdirs();
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
Toast.makeText(getContext(), file + " open failed: " + e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
return;
}
/* //method A: will get dark image
setDrawingCacheEnabled(true);
Bitmap b = getDrawingCache();
//b.setHasAlpha(true); //to set transparency
//Canvas canvas = new Canvas(b);
//canvas.drawColor(cfgBackground)); //trying to solve darken image
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
setDrawingCacheEnabled(false); //for next screen update
*/
Bitmap b = screenShot(this); //Method B
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
try {
fos.flush();
fos.close();
} catch (IOException e) {
Toast.makeText(getContext(), file + " save failed: " + e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Toast.makeText(getContext(), file + " saved in Albums", Toast.LENGTH_SHORT).show();
//add to gallary
getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.toString())));
Two functions:
//Method C
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(cfgBackground);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
//Method B
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Anyone encountered this problem?
I finally found the solution (although not sure why): the background color should be 0xFFrrggbb, eg. 0xffeeee00, though the background looks ugly but just needs more finetune...
If you know why, kindly share here, appreciated!

Android Creating an image from an API response [duplicate]

I need to create .jpeg/.png file on my Android application programmatically. I have simple image (black background), and it need to write some text on it programmatically. How can I do it? Is it possible?
It's definately possible.
To write text on an image you have to load the image in to a Bitmap object. Then draw on that bitmap with the Canvas and Paint functions. When you're done drawing you simply output the Bitmap to a file.
If you're just using a black background, it's probably better for you to simply create a blank bitmap on a canvas, fill it black, draw text and then dump to a Bitmap.
I used this tutorial to learn the basics of the canvas and paint.
This is the code that you'll be looking for to turn the canvas in to an image file:
OutputStream os = null;
try {
File file = new File(dir, "image" + System.currentTimeMillis() + ".png");
os = new FileOutputStream(file);
finalBMP.compress(CompressFormat.PNG, 100, os);
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
screenGrabFilePath = file.getPath();
} catch(IOException e) {
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
Log.e("combineImages", "problem combining images", e);
}
Yes, see here
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
You can also use awt's Graphics2D with this compatibility project
Using Graphics2d you can create a PNG image as well:
public class Imagetest {
public static void main(String[] args) throws IOException {
File path = new File("image/base/path");
BufferedImage img = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.YELLOW);
g2d.drawLine(0, 0, 50, 50);
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 50, 0, 100);
g2d.setColor(Color.RED);
g2d.drawLine(50, 50, 100, 0);
g2d.setColor(Color.GREEN);
g2d.drawLine(50, 50, 100, 100);
ImageIO.write(img, "PNG", new File(path, "1.png"));
}
}

Save layout as an image

I have a LinearLayout and I am want to save the contents of that view as an image. I have it half working.
File imageFile;
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/a.png";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
Bitmap bitmap;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
With the above code it will save a copy of the view, but only what's seen on the device screen. I have more information on the view that I want saved. The code above from here and this will only save the information seen on the screen as an image. I want all the information saved, even the information that is not on the screen (where you need to scroll to see).
How can I achieve that?
Another option is to use:
Bitmap b = Bitmap.createBitmap(width, height....)
v1.setLayoutParams // Full width and height of content
Canvas c = new Canvas(b);
v1.draw(c); // You now have full bitmap
saveBitmap(b);
Run a measure/layout pass on it and draw it to a canvas. Suppose your parent was called "view" and was a vertical LinearLayout:
view.measure(someWidth, MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(c);

capture bitmap from view android [duplicate]

This question already has answers here:
Convert view to bitmap on Android
(9 answers)
Closed 1 year ago.
I have posted same question but it's in near to my problem that's why i have posted it second time
Hi i want to capture image from RelativeLayout, for that i have used below code
captureRelativeLayout.setDrawingCacheEnabled(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(
Config.ARGB_8888, false);
the problem is that when i start activity and get image from that view at that time it will work fine, but if i use it second time, the image is not being refreshed, means that previous bitmap is every time i getting.
Now if i close my activity and agian start it then i will get updated image but again not in second time
:( for more information look at the
can't share image properly android
Here are two ways to convert a view to a bitmap:
Use Drawing Cache:
RelativeLayout view = (RelativeLayout)findViewById(R.id.relativelayout);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
I've had some issue with the drawing cache method when the view is very large (for example, a TextView in a ScrollView that goes far off the visable screen). In that case, using the next method would be better.
Use Canvas:
RelativeLayout view = (RelativeLayout)findViewById(R.id.relativelayout);
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
You should destroy the drawing cache after copying it, so the cache will be built again next time you call getDrawingCache().
The code would look like this:
captureRelativeLayout.setDrawingCacheEnabled(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(Config.ARGB_8888, false);
captureRelativeLayout.destroyDrawingCache();
Or like this if you don't want to enable the flag:
captureRelativeLayout.buildDrawingCache(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(Config.ARGB_8888, false);
captureRelativeLayout.destroyDrawingCache();
Reference: https://groups.google.com/d/msg/android-developers/IkRXuMtOA5w/zlP6SKlfX-0J
There is a Kotlin extension function in Android KTX:
val config: Bitmap.Config = Bitmap.Config.ARGB_8888
val bitmap = canvasView.drawToBitmap(config)
finally i got solution from this view.getDrawingCache() only works once
i just forget to put
captureRelativeLayout.setDrawingCacheEnabled(false);
i have done it by below code,
try {
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
captureRelativeLayout.setDrawingCacheEnabled(true);
File sdcard = Environment.getExternalStorageDirectory();
File f = new File(sdcard, "temp.jpg");
FileOutputStream out = null;
out = new FileOutputStream(f);
captureRelativeLayout.setDrawingCacheEnabled(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(
Config.ARGB_8888, false);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
captureRelativeLayout.setDrawingCacheEnabled(false);
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
startActivity(Intent.createChooser(sharingIntent, "Share Image"));
} catch (Exception e) {
e.printStackTrace();
}
As DrawingCache is depracated then in kotlin:
fun View.createBitmap(): Bitmap {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
Canvas(bitmap).apply {
background?.draw(this) ?: this.drawColor(Color.WHITE)
draw(this)
}
return bitmap
}
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
"bitmap" is the final bitmap..

fetching the drawing done on canvas using getRootView

I am using MyView for drawing content on a canvas using FingerPaint API demo app. I want to capture whatever I have written on the canvas. But when I use View v1 = myview.getRootView() it is returning only the blank canvas and not the content. I want to save my drawing in SDCard. Following is my code. Let me know what do i need to change
v1 = myview.getRootView();
System.out.println("v1 value = "+v1);
v1.buildDrawingCache(true);
v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//v1.layout(0, 0, v1.getMeasuredWidth(), v1.getMeasuredHeight());
v1.layout(0, 0, 100, 100);
//Bitmap b = Bitmap.createBitmap(v1.getDrawingCache());
myview.mBitmap = Bitmap.createBitmap(v1.getDrawingCache());
System.out.println("BITMAP VALue = "+myview.mBitmap);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
//b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (Exception e)
{
e.printStackTrace();
}
v1.setDrawingCacheEnabled(false);
myview is an object of class MyView that extends View.
you can directly use the myview object to get the current view/canvas
myview.setDrawingCacheEnabled(true);
Bitmap b = myview.getDrawingCache();
for immutable image use this
Bitmap b = myview.getDrawingCache().copy(Config.ARGB_8888, true);
and then you can save this bitmap as image file

Categories

Resources