i'm developing an android app where I can draw on a canvas. I want to convert my canvas to bitmap and then save it in jpeg format on my sd card..
how can i properly do this?
Something like that should work :
http://developer.android.com/reference/android/view/View.html#getDrawingCache(boolean)
public void toJPEGFile(){
File folder = new File(Environment.getExternalStorageDirectory()+"/folder/");
if(!folder.exists()) folder.mkdirs();
try {
this.setDrawingCacheEnabled(true);
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file"));
Bitmap bitmap = this.getDrawingCache();
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Did you look up the documentation?
Canvas API
You can use the:
public Canvas (Bitmap bitmap)
Since: API Level 1
Construct a canvas with the specified bitmap to draw into. The bitmap must be mutable.
The initial target density of the canvas is the same as the given bitmap's density.
Parameters
bitmap Specifies a mutable bitmap for the canvas to draw into.
Related
I am working with Canvas in Android Studio, have .png image on canvas and want to save it to sd card. Is it possible?
if yes then how?
thanks and regards.
This code may help you (Saving canvas to bitmap on Android)
Bitmap toDisk = null;
try {
// TODO: Get the size of the canvas, replace the 640, 480
toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);
canvas.setBitmap(toDisk);
toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("arun.jpg")));
} catch (Exception ex) {
}
You should have created Canvas with new Canvas(myBitmap);. So when you draw on the Canvas, it draws to your bitmap.
String fileName = Environment.getExternalStorageDirectory() + "/test.png";
OutputStream stream = new FileOutputStream(fileName);
/* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
myBitmap.compress(CompressFormat.PNG, 80, stream);
stream.close();
I need take screenshot of entire screen in Android, I've searched a lot but they all talked about taking screenshot of specified view, how can I take screenshot of entire screen?
I mean, by program.(Not by DDMS)
There is a library available for taking snapshot through the device its called ASL(Android Screenshot library).
Have a look here with complete source code
In eclipse go to DDMS perspective and select your device. Then click on screen capture(camera picture) button.
Go through this link it may be helpful for you...
Try below code:
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
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.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Refer to this answer.
You need to root your device otherwise it won't work. Also u have to make ur application get SuperUser access. Just implement this code and you will be good to go:
public void screenShot() throws InterruptedException
{
Process sh;
try
{
sh = Runtime.getRuntime().exec("su", null, null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/Image.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This code return screenshot of visible and unvisible part of layout.
private Bitmap getScreenBitmap() {
View v = getWindow().getDecorView().findViewById(android.R.id.content);
v.setDrawingCacheEnabled(true);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(measureSpec, measureSpec);
width = v.getMeasuredWidth();
height = v.getMeasuredHeight();
v.layout(0, 0, width, height);
v.buildDrawingCache(true);
//Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
v.draw(canvas);
v.setDrawingCacheEnabled(false);
return b;
}
In Eclipse go to Window -> Show View -> Other -> Devices
Select your device and then simply click on "camera picture":
I am drawing a text on bitmap in android application and then i am saving it in sd-card.
the image getting saved but there is no text, i mean it seems that there some problem in drawtext, this is my code
Bitmap bitmap = Bitmap.createBitmap(370, 177, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
c.drawColor(0xffffffff);
Paint p = new Paint();
p.setColor(R.color.black);
//p.setStyle(Style.FILL);
//p.setStrokeWidth(40.0f);
//p.setTextSize(40.0f);
//p.setTextAlign(Align.RIGHT);
c.drawText("Some text", 70, 77, p);
//c.save();
try {
FileOutputStream fos = new FileOutputStream(myfile);
bitmap.compress(CompressFormat.PNG, 90, fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap.recycle();
am i forget something or what's wrong with this code ?
R.color.black is not a real color but just a reference to a color value.
Replace it with Color.BLACK or getResources().getColor(R.color.black)
p.setColor(Color.BLACK);
OR
p.setColor(getResources().getColor(R.color.black));
I've got an ImageView, in its onDraw(canvas),
i tried:
canvas.drawBitmap(...);//draw an extremely large background 3264 * 2448 pixels
canvas.drawLine(...);//draw target
My question is, how can I save this canvas into a sth like png? Thanks!
From the question here:
Drawing on Canvas and save image
imgView.setDrawingCacheEnabled(true);
Bitmap b = imgView.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(getFileName());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
b.compress(CompressFormat.PNG, 95, fos);
You can save the view cache image to the disk as png.
How to save canvas drawing using SurfaceView as image not the view.
Try below code might help
Bitmap bitmap = Bitmap.createBitmap(mSurfaceView.getWidth(), mSurfaceView.getHeight(), Bitmap.Config.ARGB_8888);
mSurfaceView.draw(new Canvas(bitmap));
try {
OutputStream out = new BufferedOutputStream(new FileOutputStream(saved.png));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (IOException e) {
Log.w(TAG, e);
}