2012-02-10 52 views
0

我在做一个应用程序中,我要画在画布上的图像,然后保存在共享偏好的形象,然后显示下一屏幕。任何帮助将不胜感激安卓:实现共享偏好

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我想保存在共享偏好这一形象和开放的下一个屏幕缩略图

+0

试试这个帖子http://stackoverflow.com/questions/4388140/how-can-i-store-image-in-shared-preferencesand-retrive-it – Ajay 2012-02-10 05:08:11

+0

这是更有帮助http://stackoverflow.com/问题/ 8586242/how-can-i-store-images-using-sharedpreference-in-android – Ajay 2012-02-10 05:08:46

回答

1

如果您在SurfaceView上启用了图形缓存,则可以使用View的getDrawingCache()方法获取上一个缓存的位图。

位图对象实现了Parcelable;这意味着您可以直接将其放入Intent中,该Intent使用Intent.putExtra(键,位图)启动您的下一个活动。

+0

您可以请帮我一些示例代码.. – Aditya1510 2012-02-10 05:49:12

0

您可能能够得到从位图的字节数组,将其转换成一个字符串中的共享偏好保存。

但我认为在sqlite数据库中保存好得多。

+0

我正在分享我的代码,你能否告诉我如何从画布上保存图像 – Aditya1510 2012-02-10 05:29:17

0

可能的是,使用图像的base64字符串表示形式,然后将该字符串保存在共享首选项中,并且还可以将图像路径保存为共享首选项。

+0

我在分享我的代码。我与一些示例代码.. – Aditya1510 2012-02-10 05:28:48

0

你是否在使用sharedPreference来在另一个活动中使用图像,然后你滥用了一个功能,试图改为传递位图,或者使用静态变量,尽管我不会建议使用静态变量。

+0

你可以请帮我一些示例代码 – Aditya1510 2012-02-10 06:01:06

+0

请看看我的代码 – Aditya1510 2012-02-10 06:01:20