I am making an app in which i have to draw image in canvas and then SAVE that image in shared preference and then show in next screen .Any help will be appreciated
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawingPanel(this));
mPaint = new Paint();
mPaint.setDither(true);
// mPaint.setColor(0xFF FF FF FF);
System.out.println("hello1");
mPaint.setColor(Color.BLACK);
System.out.println("hello2");
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
@SuppressWarnings("null")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.create_new: // **ON THIS CLICK I WANT TO SAVE IMAGE**
System.out.println("hii");
// name = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA));
System.out.println("hii1");
date1 = currentTimeString;
System.out.println("hii2");
// cur = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
System.out.println("hii3");
// File f1=new File("/sdcard/" + date1 + ".png");
System.out.println("hii4");
// FileSave fs = null;
// fs.Save(f1);
// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
// Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
// FileOutputStream out = new FileOutputStream(path);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
return true;
case R.id.erase:
System.out.println("new2");
mPaint.setColor(-1);
mPaint.setAlpha(0);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(1);
Intent intent1 = new Intent(getApplicationContext(), CanvasDrawingActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
private DrawingThread _thread;
private Path path;
public DrawingPanel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new DrawingThread(getHolder(), this);
}
@Override
protected void onAttachedToWindow() {
// TODO Auto-generated method stub
super.onAttachedToWindow();
System.out.println("hello");
Save=(Button)findViewById(R.id.Save);
System.out.println("hello2");
}
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
path = new Path();
path.moveTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_UP){
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
}
return true;
}
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
for (Path path : _graphics) {
//canvas.drawPoint(graphic.x, graphic.y, mPaint);
canvas.drawPath(path, mPaint);
canvas.drawPath(path, mPaint);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
_thread.setRunning(true);
_thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
class DrawingThread extends Thread {
private SurfaceHolder _surfaceHolder;
private DrawingPanel _panel;
private boolean _run = false;
public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}
i want to save this image in shared preference and open in next screen with thumbnail
If you have drawing cache enabled on your SurfaceView, you can get the last cached Bitmap with the getDrawingCache() method of View.
The Bitmap object implements Parcelable; that means you can directly put it into the Intent that is launching your next Activity with Intent.putExtra(key, bitmap).