I want to save a canvas draw which I created by extending View
class as image in sd card .In this view I can create any line on Touching screen.
The Custom View class :
public class TouchEventView extends View {
Paint paint=new Paint();
Path path=new Path();
Bitmap myBitmap;
public TouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawPath(path, paint);
myBitmap=Bitmap.createBitmap((int)canvas.getWidth(), (int)canvas.getHeight(), Config.RGB_565);// bitmap created
canvas=new Canvas(myBitmap);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float xPos=event.getX();
float yPos=event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(xPos, yPos);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(xPos, yPos);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;
}
}
I want to save this as image in SD card .
The main class is as follows:
public class WriteOnScreenActivity extends ActionBarActivity {
private String state;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TouchEventView(this,null));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.write_on_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.savepic) {
state=Environment.getExternalStorageState();
if(state.equals(Environment.MEDIA_MOUNTED)){
File folder;
folder=new File(Environment.getExternalStorageDirectory()+"/utkpictdrawn/");
if(!folder.exists()){
folder.mkdirs();
}
}else{
Toast.makeText(getBaseContext(), "Cannot Save Image.No SD Card !", Toast.LENGTH_LONG).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
In main class I am using menu option save canvas .When I click on this the image should be saved in SD card.
I have read many threads related this but all are very complex .
So is there any easy way to save canvas draw as image according to my code in SD Card ?
I am a beginner so please explain your code answer also
One more thing I want to do all saving stuff from the onOptionsItemSelected
from menu from the main Activity NOT in the custom view
class!