1

我想使用代码截取我的透明应用程序的屏幕截图。使用此代码,我仅获得外部视图的屏幕截图。请帮忙

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) {}
4

1 回答 1

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;
}
于 2012-12-06T10:43:16.310 回答