Have you ever thought of drawing something on Canvas and sharing it with others ? If yes, then this post is for you :)
So, you know that you can draw your stuff on a Canvas view. If you want to share your work with others, you may want to save it as an image. This post shows how to convert a Canvas view into a bitmap. First "Play" menu-item will show a programmatically drawn text on canvas view. "Save" icon will save it in gallery as well as in external SD card.
Details:
First create Custom View called MyCanvas class:
package com.teach.mycanvasapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyCanvas extends View {
public MyCanvas(Context context) {
super(context);
// TODO Auto-generated constructor stub }
@Override protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub super.onDraw(canvas);
Paint pBackground = new Paint();
pBackground.setColor(Color.WHITE);
canvas.drawRect(0, 0, 512, 512, pBackground);
Paint pText = new Paint();
pText.setColor(Color.BLACK);
pText.setTextSize(20);
canvas.drawText("This is a sample canvas image", 100, 100, pText);
}
}
Second, reference MyCanvas from MainActivity class to show it inside ImageView. I've two menu-items, one is for showing the bitmap and another is to take save action :
package com.teach.mycanvasapp;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
ImageView imageView = null;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.show_canvas) {
View v = new MyCanvas(getApplicationContext());
Bitmap bitmap = Bitmap.createBitmap(500/*width*/, 500/*height*/, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
return true;
} else if (id == R.id.save_canvas) {
saveImageToGallery();
return true;
}
return super.onOptionsItemSelected(item);
}
public void saveImageToGallery() {
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
//Writing image to SD card. //It could also be saved to internal storage. // That way, we don't need to have extra permissions File dir = new File("/sdcard/tempfolder/");
try {
if (!dir.exists()) {
dir.mkdirs();
}
File output = new File(dir, "tempfile.jpg");
if (!output.exists())
output.createNewFile();
OutputStream os = null;
os = new FileOutputStream(output);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
//Scans image and save it in gallery MediaScannerConnection.scanFile(this, new String[] { output.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.d("Test", "Image saved in gallery!");
}
}
);
} catch (Exception e) {
Log.d("Test", "Exception: " + e.getMessage());
}
}
}
Here's link to the github repo:
https://github.com/ptyagi911/MyCanvasApp