2013-11-25 126 views
0
##MyDraw.java## 

This part of the code is where the bitmap image is created, the bitmap sits on the canvas, form this point I want to be able to save the image that is created on the bitmap, I have a drop down menu in the MainActivity with a 'save' option.位圖圖像保存

package com.example.save_file; 

import java.util.Random; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.view.View; 
import android.view.MotionEvent; 

public class MyDraw extends View 

{ 

Canvas c; 
Bitmap bmp; 
Paint paint; 
Random g; 
float X, Y; 

public MyDraw (Context context) 

{ 

     super(context); 

     g = new Random();   
     Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
     bmp = Bitmap.createBitmap (1100, 1800, conf);    

     paint = new Paint(); 
     paint.setStyle (Paint.Style.STROKE); 
     paint.setColor (Color.WHITE);   

     this.setOnTouchListener (new OnTouchListener() 
     { 
      public boolean onTouch (View v, MotionEvent event) 

      { 

       int h, w, R, G, B, A; 
       float x, y; 

       c = new Canvas (bmp);      

       x = event.getX(); 
       y = event.getY(); 
       System.out.printf ("%f %f\n", X, Y);      

       paint.setAntiAlias (true); 

       w = g.nextInt (70)+90; 
       h = g.nextInt (70); 

       R = g.nextInt (255); 
       G = g.nextInt (255); 
       B = g.nextInt (255); 
       A = g.nextInt (255); 

       paint.setStyle (Paint.Style.FILL); 
       paint.setColor ((A << 24) + (R << 16) + (G << 8) + (B << 0)); 

       if (MyApp.fill == 0) // FILLED SHAPE 
       { 
        paint.setStyle (Paint.Style.FILL); 
        paint.setColor ((A << 24) + (R << 16) + (G << 8) + (B << 0)); 

        if (MyApp.shape == 0) 
          c.drawRect (x, y, x + w, y + h, paint); 
        else 
          c.drawOval(new RectF (x, y, x + w, y + h), paint); 

        paint.setStyle (Paint.Style.STROKE); 
        paint.setColor (Color.BLACK);          

        if (MyApp.shape == 0) 
          c.drawRect (x, y, x + w, y + h, paint); 
       else 
          c.drawOval(new RectF (x, y, x + w, y + h), paint); 
       } 
       else // OUTLINED SHAPE 
       { 
        paint.setStyle (Paint.Style.STROKE); 
        paint.setColor ((A << 24) + (R << 16) + (G << 8) + (B << 0));    
        if (MyApp.shape == 0) 
          c.drawRect (x, y, x + w, y + h, paint); 
        else 
          c.drawOval(new RectF (x, y, x + w, y + h), paint); 
       }         
       paint.setColor (Color.WHITE); 
       invalidate();     
       return true; 

      } 


      }); 

    } 
    @Override 
    protected void onDraw (Canvas c) 
    {   
     super.onDraw (c); 
     c.drawBitmap (bmp, 0, 0, paint); 
    } 
} 

##MyApp.java## 

    package com.example.save_file; 

public class MyApp 

{ 

static public int shape = 0; 
static public int fill = 0; 

} 


##MainActivity.java## 

This is the part of the code where the menu code is, I want to be able to pres the save button here and for the bitmap image to be saved to the phone, preferably a standard gallery folder.

package com.example.save_file; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.util.Random; 

import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.CompressFormat; 
import android.view.Menu; 
//import android.gesture.GestureOverlayView; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     MyDraw d = new MyDraw (this); 
     setContentView (d); 
    }  

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
      super.onCreateOptionsMenu (menu); 
      MenuItem menu1 = menu.add(0, 0, Menu.NONE, "Filled Shape"); 
      MenuItem menu2 = menu.add(0, 1, Menu.NONE, "Outline Shape"); 
      MenuItem menu3 = menu.add(0, 2, Menu.NONE, "Rectangle"); 
      MenuItem menu4 = menu.add(0, 3, Menu.NONE, "Oval"); 
      MenuItem menu5 = menu.add(0, 4, Menu.NONE, "Save!"); 

      return true; 
    } 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
      switch (item.getItemId()) 

      { 
      case 0: 
       MyApp.fill = 0; 
       return true; 
      case 1: 
       MyApp.fill = 1; 
       return true; 
      case 2: 
       MyApp.shape = 0; 
       return true; 
      case 3: 
       MyApp.shape = 1; 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 

      case 4: 

       bmp.setDrawingCacheEnabled(true); 
       Bitmap bitmap = bmp.getDrawingCache(); 
       File root = Environment.getExternalStorageDirectory(); 
       File file = new 
        File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg"); 
       try 
       { 
        file.createNewFile(); 
        FileOutputStream ostream = new FileOutputStream(file); 
        bitmap.compress(CompressFormat.JPEG, 100, ostream); 
        ostream.close(); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
        } 
    } 

}

+0

謝謝,我覺得從它加入你的評論。 –

回答

0

我用這個代碼來保存任何圖形使用帆布drawed,像fingerpaint或類似的,我希望可以爲你有用案例

將實例設置爲全局

MyDraw d; 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    d = new MyDraw (this); 
    setContentView (d); 
} 

把這個放在onOptionsItemSelected()的情況下;

case 4: 
    try { 
     Bitmap bitmap = Bitmap.createBitmap(d.getWidth(),d.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     d.draw(canvas); 
     File folder = new File(Environment.getExternalStorageDirectory()+ "/DCIM/Camera/"); 
     if (!folder.exists()) 
      folder.mkdirs(); 
     String fileName = Environment.getExternalStorageDirectory()+ "/DCIM/Camera/img.jpg""; 
     if (new File(fileName).exists()) 
      new File(fileName).delete(); 
     OutputStream stream = new FileOutputStream(fileName); 
     /* 
     * Write bitmap to file using JPEG or PNG and 100% quality hint 
     * for JPEG. 
     */ 
     bitmap.compress(CompressFormat.JPEG, 100, stream); 
     stream.close(); 
     } catch (Exception e) { 
       // TODO Auto-generated catch block 
       Toast.makeText(this, "Error: " + e.getMessage(),Toast.LENGTH_LONG).show(); 
     } 
     break; 

UPDATE: 檢查您的模擬設備具有SD卡 enter image description here

提醒你要添加的權限來體現

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

不,在MainActivity中,我給你的所有代碼都不在myDraw.java中 –

+0

我已經完成了所有這些,但是我只有模擬器和一個沒有SD卡的HTC,你有沒有在模擬器上工作?在選擇保存按鈕之後,出現錯誤消息,錯誤:/ storage/emulated/0/DCIM/Camera/img:打開失敗:EACCES(Permission denied)感謝您的幫助。 –

+0

heck更新 –