2013-10-07 73 views
0

在我的活動中,我可以製作簽名圖並使用「saveToDatabase」按鈕將其保存在SQLite數據庫中。當我重新打開活動時,我想從我的數據庫中加載繪圖,但我無法弄清楚如何操作!誰能幫忙?我已經在「從數據庫添加簽名」中解碼位圖,但接下來我需要做什麼?我已經在我的數據庫的位圖中保存了一幅圖畫,我該如何重繪它?

這裏是我的代碼:

public class WerkbonDetailsActivity extends Activity{ 

public Werkbon chosenWorkorder; 
Button mClear, mGetSign, saveToDatabase; 
LinearLayout mContent; 
signature mSignature; 
public static String tempDir; 
public int count = 1; 
public String current = null; 
private Bitmap mBitmap; 
View mView; 
Database db = new Database(this); 
int chosenWorkorderposition = 0; 
Paint paintPieFill; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_werkbondetails); 



    tempDir = Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.external_dir) + "/"; 
    prepareDirectory(); 
    current = count + ".png"; 
    mContent = (LinearLayout) findViewById(R.id.linearLayout); 
    mSignature = new signature(this, null); 
    mSignature.setBackgroundColor(Color.WHITE); 
    mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    mClear = (Button)findViewById(R.id.btn_handtekeningwissen); 
    mGetSign = (Button)findViewById(R.id.getsign); 
    saveToDatabase = (Button)findViewById(R.id.save); 
    mView = mContent; 

    Bundle extras = getIntent().getExtras(); 

    if (extras != null) { 
     chosenWorkorderposition = (int)extras.getInt("chosenWorkorderposition"); 
    } 
    chosenWorkorder = db.getWerkbon(chosenWorkorderposition + 1); 


    mClear.setOnClickListener(new OnClickListener() 
    {   
     public void onClick(View v) 
     { 
      Log.v("log_tag", "Panel Cleared"); 
      mSignature.clear(); 
     } 
    }); 

    mGetSign.setOnClickListener(new OnClickListener() 
    {   
     public void onClick(View v) 
     { 
      Log.v("log_tag", "Panel Saved"); 
      mView.setDrawingCacheEnabled(true); 
      mSignature.save(mView); 
     } 
    }); 

    saveToDatabase.setOnClickListener(new OnClickListener() 
    {   
     public void onClick(View v) 
     { 
     mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);; 
     Canvas canvas = new Canvas(mBitmap); 
     v.draw(canvas); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byte[] bitmapdata = stream.toByteArray(); 
     chosenWorkorder.setHandtekening(bitmapdata); 

     db.updateWerkbon(chosenWorkorder); 
     } 
    }); 

    //Adding Signature from database 
    if(chosenWorkorder.getHandtekening() != null){ 
     ByteArrayInputStream stream = new ByteArrayInputStream(chosenWorkorder.getHandtekening()); 
     Bitmap bm = BitmapFactory.decodeStream(stream); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

private boolean prepareDirectory() 
{ 
    try 
    { 
     if (makedirs()) 
     { 
      return true; 
     } else { 
      return false; 
     } 
    } catch (Exception e) 
    { 
     e.printStackTrace(); 
     Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", 1000).show(); 
     return false; 
    } 
} 

private boolean makedirs() 
{ 
    File tempdir = new File(tempDir); 
    if (!tempdir.exists()) 
     tempdir.mkdirs(); 

    if (tempdir.isDirectory()) 
    { 
     File[] files = tempdir.listFiles(); 
     for (File file : files) 
     { 
      if (!file.delete()) 
      { 
       System.out.println("Failed to delete " + file); 
      } 
     } 
    } 
    return (tempdir.isDirectory()); 
} 


public class signature extends View 
{ 
    private static final float STROKE_WIDTH = 5f; 
    private static final float HALF_STROKE_WIDTH = STROKE_WIDTH/2; 
    private Paint paint = new Paint(); 
    private Path path = new Path(); 

    private float lastTouchX; 
    private float lastTouchY; 
    private final RectF dirtyRect = new RectF(); 

    public signature(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     paint.setAntiAlias(true); 
     paint.setColor(Color.BLUE); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(STROKE_WIDTH); 
    } 

    public void save(View v) 
    { 
     Log.v("log_tag", "Width: " + v.getWidth()); 
     Log.v("log_tag", "Height: " + v.getHeight()); 
     if(mBitmap == null) 
     { 
      mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);; 
     } 
     Canvas canvas = new Canvas(mBitmap); 
     String FtoSave = tempDir + current; 
     File file = new File(FtoSave); 
     try 
     { 
      FileOutputStream mFileOutStream = new FileOutputStream(file); 
      v.draw(canvas); 
      mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream); 
      mFileOutStream.flush(); 
      mFileOutStream.close(); 
      String url = Images.Media.insertImage(getContentResolver(), mBitmap, "title", null); 
      Log.v("log_tag","url" + url); 
     } 
     catch(Exception e) 
     { 
      Log.v("log_tag", e.toString()); 
     } 
    } 

    public void clear() 
    { 
     path.reset(); 
     invalidate(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     canvas.drawPath(path, paint); 
    } 

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

     switch (event.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
       path.moveTo(eventX, eventY); 
       lastTouchX = eventX; 
       lastTouchY = eventY; 
       return true; 

      case MotionEvent.ACTION_MOVE: 

      case MotionEvent.ACTION_UP: 
       resetDirtyRect(eventX, eventY); 
       int historySize = event.getHistorySize(); 
       for (int i = 0; i < historySize; i++) 
       { 
         float historicalX = event.getHistoricalX(i); 
         float historicalY = event.getHistoricalY(i); 
         expandDirtyRect(historicalX, historicalY); 
         path.lineTo(historicalX, historicalY); 
       } 
       path.lineTo(eventX, eventY); 
       break; 

      default: 
       debug("Ignored touch event: " + event.toString()); 
       return false; 
     } 

     invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH), 
      (int) (dirtyRect.top - HALF_STROKE_WIDTH), 
      (int) (dirtyRect.right + HALF_STROKE_WIDTH), 
      (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); 

     lastTouchX = eventX; 
     lastTouchY = eventY; 

     return true; 
    } 

    private void debug(String string) 
    { 
    } 

    private void expandDirtyRect(float historicalX, float historicalY) 
    { 
     if (historicalX < dirtyRect.left) 
     { 
      dirtyRect.left = historicalX; 
     } 
     else if (historicalX > dirtyRect.right) 
     { 
      dirtyRect.right = historicalX; 
     } 

     if (historicalY < dirtyRect.top) 
     { 
      dirtyRect.top = historicalY; 
     } 
     else if (historicalY > dirtyRect.bottom) 
     { 
      dirtyRect.bottom = historicalY; 
     } 
    } 

    private void resetDirtyRect(float eventX, float eventY) 
    { 
     dirtyRect.left = Math.min(lastTouchX, eventX); 
     dirtyRect.right = Math.max(lastTouchX, eventX); 
     dirtyRect.top = Math.min(lastTouchY, eventY); 
     dirtyRect.bottom = Math.max(lastTouchY, eventY); 
    } 
}//signature 
} 

回答

0

從數據庫中檢索字節數組,你可以用這個方法解碼:

private static Bitmap deserialize(byte[] data) throws IOException, 
     ClassNotFoundException { 
    ByteArrayInputStream in = new ByteArrayInputStream(data); 
    ObjectInputStream is = new ObjectInputStream(in); 
    return (Bitmap) is.readObject(); 
} 
+0

我無法得到它的工作這樣!我需要把這個放在哪裏? – Remi

相關問題