2013-03-27 50 views
0

在API中,Demos是一个名为FingerPaint的绘图示例。Android保存图/

package com.example.android.apis.graphics; 

import android.content.Context; 
import android.graphics.*; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 

public class FingerPaint extends GraphicsActivity 
     implements ColorPickerDialog.OnColorChangedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new MyView(this)); 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(0xFFFF0000); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(12); 

     mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 
             0.4f, 6, 3.5f); 

     mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); 
    } 

    private Paint  mPaint; 
    private MaskFilter mEmboss; 
    private MaskFilter mBlur; 

    public void colorChanged(int color) { 
     mPaint.setColor(color); 
    } 

    public class MyView extends View { 

     private static final float MINP = 0.25f; 
     private static final float MAXP = 0.75f; 

     private Bitmap mBitmap; 
     private Canvas mCanvas; 
     private Path mPath; 
     private Paint mBitmapPaint; 

     public MyView(Context c) { 
      super(c); 

      mPath = new Path(); 
      mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
     } 

     @Override 
     protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
      super.onSizeChanged(w, h, oldw, oldh); 
      mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(mBitmap); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      canvas.drawColor(0xFFAAAAAA); 

      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 

      canvas.drawPath(mPath, mPaint); 
     } 

     private float mX, mY; 
     private static final float TOUCH_TOLERANCE = 4; 

     private void touch_start(float x, float y) { 
      mPath.reset(); 
      mPath.moveTo(x, y); 
      mX = x; 
      mY = y; 
     } 
     private void touch_move(float x, float y) { 
      float dx = Math.abs(x - mX); 
      float dy = Math.abs(y - mY); 
      if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
       mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
       mX = x; 
       mY = y; 
      } 
     } 
     private void touch_up() { 
      mPath.lineTo(mX, mY); 
      // commit the path to our offscreen 
      mCanvas.drawPath(mPath, mPaint); 
      // kill this so we don't double draw 
      mPath.reset(); 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event) { 
      float x = event.getX(); 
      float y = event.getY(); 

      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        touch_start(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        touch_move(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_UP: 
        touch_up(); 
        invalidate(); 
        break; 
      } 
      return true; 
     } 
    } 

    private static final int COLOR_MENU_ID = Menu.FIRST; 
    private static final int EMBOSS_MENU_ID = Menu.FIRST + 1; 
    private static final int BLUR_MENU_ID = Menu.FIRST + 2; 
    private static final int ERASE_MENU_ID = Menu.FIRST + 3; 
    private static final int SRCATOP_MENU_ID = Menu.FIRST + 4; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 

     menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c'); 
     menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's'); 
     menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z'); 
     menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z'); 
     menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z'); 

     /**** Is this the mechanism to extend with filter effects? 
     Intent intent = new Intent(null, getIntent().getData()); 
     intent.addCategory(Intent.CATEGORY_ALTERNATIVE); 
     menu.addIntentOptions(
           Menu.ALTERNATIVE, 0, 
           new ComponentName(this, NotesList.class), 
           null, intent, 0, null); 
     *****/ 
     return true; 
    } 

    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     super.onPrepareOptionsMenu(menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     mPaint.setXfermode(null); 
     mPaint.setAlpha(0xFF); 

     switch (item.getItemId()) { 
      case COLOR_MENU_ID: 
       new ColorPickerDialog(this, this, mPaint.getColor()).show(); 
       return true; 
      case EMBOSS_MENU_ID: 
       if (mPaint.getMaskFilter() != mEmboss) { 
        mPaint.setMaskFilter(mEmboss); 
       } else { 
        mPaint.setMaskFilter(null); 
       } 
       return true; 
      case BLUR_MENU_ID: 
       if (mPaint.getMaskFilter() != mBlur) { 
        mPaint.setMaskFilter(mBlur); 
       } else { 
        mPaint.setMaskFilter(null); 
       } 
       return true; 
      case ERASE_MENU_ID: 
       mPaint.setXfermode(new PorterDuffXfermode(
                 PorterDuff.Mode.CLEAR)); 
       return true; 
      case SRCATOP_MENU_ID: 
       mPaint.setXfermode(new PorterDuffXfermode(
                PorterDuff.Mode.SRC_ATOP)); 
       mPaint.setAlpha(0x80); 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

我想绘制的图像保存诸如PNG文件到手机上。我添加了以下代码:

case SAVE_MENU_ID: 
     String filename = "signature.png"; 
     getWindow().getDecorView().getRootView() 
       .setDrawingCacheEnabled(true); 
     Bitmap b = getWindow().getDecorView().getRootView() 
       .getDrawingCache(); 

     OutputStream stream = null; 
     try { 
      stream = new FileOutputStream("/sdcard/signature.png"); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     /* 
     * Write bitmap to file using JPEG or PNG and 80% quality hint for 
     * JPEG. 
     */ 
     // bitmap.compress(CompressFormat.PNG, 80, stream); 
     try { 
      stream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

image,signature.png正在保存到手机中,但它的大小为0字节。我已添加到我的清单中。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+3

您应该取消注释实际保存图像的行。 – 323go 2013-03-27 17:00:11

+0

你能确保你的'位图b'不是空的吗? (我的意思是它应该有一些数据里面,但不是空) – dumbfingers 2013-03-27 17:00:49

+0

@ ss1271,没有位图b不是空的 – BullShark 2013-03-27 17:49:44

回答

0

岂不是

 stream = new FileOutputStream(file); 

       than 

     stream = new FileOutputStream("/sdcard/signature.png"); 

     FileOutputStream(File file) 
     Constructs a new FileOutputStream that writes to file. 

http://developer.android.com/reference/java/io/FileOutputStream.html

还要取消以下

 bitmap.compress(CompressFormat.PNG, 80, stream); 

可以使用alertdialog允许用户提供一个名称被保存抽奖。

一旦用户输入名称并单击确定,您可以将该绘图保存在SD卡下的名称下。

在我的代码内容是rootview;

case SAVE_MENU_ID: 

       AlertDialog.Builder editalert = new AlertDialog.Builder(DrawingRoomScreen.this); 
       editalert.setTitle("Please Enter the name with which you want to Save"); 
       final EditText input = new EditText(DrawingRoomScreen.this); 
       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.FILL_PARENT, 
         LinearLayout.LayoutParams.FILL_PARENT); 
       input.setLayoutParams(lp); 
       editalert.setView(input); 
       editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         content.setDrawingCacheEnabled(true); 
         String name= input.getText().toString(); 
         Bitmap bitmap = content.getDrawingCache(); 

        String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
         File file = new File("/sdcard/"+name+".png");   
         try 
         { 
          if(!file.exists()) 
         { 
          file.createNewFile(); 
         } 
          FileOutputStream ostream = new FileOutputStream(file); 
          bitmap.compress(CompressFormat.PNG, 10, ostream); 
          System.out.println("saving......................................................"+path); 
          ostream.close(); 
          content.invalidate();       
         } 
         catch (Exception e) 
         { 
          e.printStackTrace(); 
         }finally 
         { 

          content.setDrawingCacheEnabled(false);       
         } 
        } 
       }); 

       editalert.show(); 

    break;