2012-12-06 113 views
1

我想使用代碼拍攝透明應用程序的屏幕截圖。使用這段代碼,我只能看到外部視圖的屏幕截圖。請幫助Android中的透明應用程序的屏幕截圖

View v = getWindow().getDecorView().getRootView(); 
v.setDrawingCacheEnabled(true); 
Bitmap b = v.getDrawingCache(); 
try{ 
    File sdcard = Environment.getExternalStorageDirectory(); 
    File pictureDir = new File(sdcard, "picopaint"); 
    pictureDir.mkdirs(); 
    File f = null; 
    for (int i = 1; i < 200; ++i){ 
     String name = "screen" + i + ".png"; 
     f = new File(pictureDir, name); 
     if (!f.exists()) {               
      break; 
     } 
    } 
    if (!f.exists()) { 
     String name = f.getAbsolutePath(); 
     FileOutputStream fos = new FileOutputStream(name); 
     b.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.flush(); 
     fos.close();             
    } 
} catch (Exception e) {} 
+0

你現在得到修改? – Ishtiaq

+0

你有沒有找到解決方案? – Wops

+0

我也有這個問題你有代碼爲 – anddevmanu

回答

0

試試這個,這是幹活,我生成文件...您可以按照您的要求

public Bitmap generateFileFromView(View v){ 

Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); 
Canvas c = new Canvas(bmp); // v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); 
v.draw(c); 
// create a file to write bitmap data 
File file = new File(context.getCacheDir(), "f"); 
file.createNewFile(); 

    // Convert bitmap to byte array 
Bitmap bitmap = bmp; 
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos); 
byte[] bitmapdata = bos.toByteArray(); 

    // write the bytes in file 
FileOutputStream fos = new FileOutputStream(file); 
fos.write(bitmapdata); 
return file; 
} 
+0

不適合我 –

相關問題