1

我正在尝试保存我的应用程序的屏幕截图。我的主屏幕是 SurfaceView,我正在创建一个新画布并将 SurfaceView 绘制到画布中。我有一个问题,因为我得到的 PNG 是完全透明的。

这是我的代码

Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(image);
    draw(c);

    String path=Environment.getExternalStorageDirectory() + "/test2.png";
    File file = new File(path);
    try 
    {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        image.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
        Uri screenshotUri = Uri.parse("file://"+file.getAbsolutePath());
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setDataAndType(screenshotUri, "image/png");
       startActivity(sendIntent);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
4

1 回答 1

0

我认为您需要确保通过执行此操作已完全创建表面

http://www.edu4java.com/androidgame/androidgame3.html

尝试在 onSurfaceCreated 方法中截取屏幕截图,否则在表面存在之前您将变为空白。文档说 draw() 在呈现完整布局之前不会返回任何内容。

于 2011-08-15T22:04:48.000 回答