我正在创建一个应用程序,我必须在画布中绘制图像,然后以共享首选项保存该图像,然后在下一个屏幕中显示。任何帮助将不胜感激
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);
}
}
}
}
}
}
我希望将此图片保存在共享首选项中,并在下一个屏幕中使用缩略图
打开答案 0 :(得分:1)
如果您在SurfaceView上启用了绘图缓存,则可以使用View的getDrawingCache()方法获取最后一个缓存的Bitmap。
Bitmap对象实现了Parcelable;这意味着您可以直接将它放入使用Intent.putExtra(键,位图)启动下一个Activity的Intent。
答案 1 :(得分:0)
您可以从位图中获取字节数组,并将其转换为字符串以保存在共享首选项中。
但我认为在sqlite数据库中保存更好。
答案 2 :(得分:0)
可以使用图像的base64字符串表示,然后以共享首选项保存字符串,也可以将图像路径保存为共享首选项。
答案 3 :(得分:0)
你是否只是在另一个活动中使用sharedPreference,然后你滥用一个功能,尝试传递位图,或者使用静态变量,虽然我不建议使用静态变量。