4

我正在使用開發者網站的這個API演示,THIS DEMO.如何在android中保存繪圖畫布?

但我想知道如何將該圖像保存到My Andrtoid設備中。 請任何人提供代碼以將繪製的圖像保存到Android設備。

謝謝。

+1

訪問關注[鏈接] [1]爲答案。 [1]:http://stackoverflow.com/questions/2174875/android-canvas-to-jpg – Lucifer

回答

12

試試這個代碼

View content = your_view; 
content.setDrawingCacheEnabled(true); 
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 
Bitmap bitmap = content.getDrawingCache(); 
String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
File file = new File(path+"/image.png"); 
FileOutputStream ostream; 
try { 
    file.createNewFile(); 
    ostream = new FileOutputStream(file); 
    bitmap.compress(CompressFormat.PNG, 100, ostream); 
    ostream.flush(); 
    ostream.close(); 
    Toast.makeText(getApplicationContext(), "image saved", 5000).show(); 
} catch (Exception e) { 
    e.printStackTrace(); 
    Toast.makeText(getApplicationContext(), "error", 5000).show(); 
} 
+1

如何我還可以節省的'view'背景?現在,這只是保存繪圖,而不是背景。 – Si8

+0

答案說查看內容= you_view;糾正我如果我錯了,我應該把我的佈局ID或視圖ID?我做了這個View content = findViewById(R.id.content);我得到了「試圖調用虛擬方法'void android.view.View.setDrawingCacheEnabled(boolean)'對一個空對象引用」 –

0

一個選項是創建另一個畫布(如下所示)並在這個新畫布上重複所有繪圖。 完成後,調用drawBitmap。

Bitmap bitmap = new Bitmap(// Set the params you like //); 
Canvas canvas = new Canvas(bitmap); 

// Do all your drawings here 

canvas.drawBitmap(// The first picture //); 

最好的是,如果有複製現有畫布的方式,然後你不會需要重新繪製的一切,但我找不到一個。

1
drawView.setDrawingCacheEnabled(true); 
Bitmap bm = null; 
drawView.destroyDrawingCache(); 
bm=drawView.getDrawingCache(); 

然後寫的位圖使用位圖工廠到文件。