9
我有一個View
,我想將其轉換爲圖像以便將其存儲在某個地方。 但是,我怎樣才能將此View
轉換爲圖像?如何將視圖轉換爲Drawable?
我有一個View
,我想將其轉換爲圖像以便將其存儲在某個地方。 但是,我怎樣才能將此View
轉換爲圖像?如何將視圖轉換爲Drawable?
嘗試此觀點,並存儲在SD卡將影像..
View view = TextView.getRootView();
//You can use any view of your View instead of TextView
if (view != null)
{
System.out.println("view is not null.....");
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
try
{
if (bm != null)
{
String dir = Environment.getExternalStorageDirectory().toString();
System.out.println("bm is not null.....");
OutputStream fos = null;
File file = new File(dir,"sample.JPEG");
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
bos.flush();
bos.close();
}
}
catch(Exception e)
{
System.out.println("Error="+e);
e.printStackTrace();
}
}
啓用視圖繪製緩存:
view.setDrawingCacheEnabled(true);
從緩存中創建位圖:
bitmap = Bitmap.createBitmap(view.getDrawingCache());
保存位圖的地方......
禁用繪圖緩存:
view.setDrawingCacheEnabled(false);
我得到一個NullPointerException異常** **此行代碼'位圖BM = Bitmap.createBitmap (view.getDrawingCache());'可能是什麼原因? – AnujAroshA 2012-09-06 06:27:42
找到**解決方案**。訪問[這裏](http://stackoverflow.com/a/4618030/833007) – AnujAroshA 2012-09-06 07:02:40