2012-02-10 11 views
0

を実装私はキャンバスに画像を描画する必要があり、その後、共有優先してその画像を保存し、次の画面の.ANYのヘルプに表示されるアプリはアンドロイド:共有優先

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

+0

これはより役に立ちますhttp://stackoverflow.com/質問/ 8586242 /どのように--I-ストア画像-使用-sharedpreference・イン・アンドロイドできる – Ajay

答えて

1

あなたのSurfaceViewで有効になってキャッシュを描画している場合は、ビューのgetDrawingCache()メソッドで最後にキャッシュされたビットマップを取得することができます。

BitmapオブジェクトはParcelableを実装しています。 Intent.putExtra(キー、ビットマップ)で次のアクティビティを起動しているIntentに直接入れることができます。

+0

いくつかのサンプルコードで私を助けてください。 – Aditya1510

0

で次の画面で共有好みとオープンでこの画像を保存したいです。

しかし、私はSQLiteのデータベースに保存すると、より多くの方が良いと思います。

+0

私は私のコードを共有しています、あなたが助け.Can私は私のコードを共有していますどのようにキャンバス – Aditya1510

0

なお、画像のbase64文字列表現を使用して、共有好みの文字列を保存します。また、共有好みの画像パスを保存することができ、可能です。

+0

から画像を保存する方法を教えpleseはでき私はいくつかのサンプルコードを持っています。 – Aditya1510

0

あなたは、私が文句を言わない静的変数を使用することをお勧めのにちょうど、ビットマップを渡す、または静的変数を使用する代わりに、意図しようとし、その後、この機能を悪用している、別のアクティビティにイメージを使用するsharedPreferenceを使用しています。

+0

いくつかのサンプルコードで私を助けてください。 – Aditya1510

+0

私のコードを見てください。 – Aditya1510

関連する問題