简体   繁体   English

Android:如何使用画布捕获当前的全屏

[英]Android: How can I capture current fullscreen using canvas

Somebody please help me with this, I have tried capturing fullscreen without canvas and I get a black screen image each time. 有人请帮助我,我已经尝试在没有画布的情况下捕获全屏,每次都会得到一个黑屏图像。 Another interesting thing I have found is that when I open that image on a mobile and delete it, then another screen with an image appears. 我发现的另一件有趣的事情是,当我在手机上打开该图像并将其删除时,会出现另一个带有图像的屏幕。 How it happened I don't know as I am new in android app development. 怎么回事我不知道因为我是Android应用程序开发的新手。

Can anyone please help me find a solution to this? 有人可以帮我找到解决方案吗?

A quick search here in SO gave me this which has an accepted answer. 在SO中快速搜索给了我这个已经接受的答案。 You might want to look at it. 你可能想看看它。 SO Source: Taking Screenshot SO来源: 截图

Quoting: 引用:

You can do it like this, 你可以这样做,

Give the id for your main Layout & after you display the content on the screen write the below code on some Listener say button click or menu item or any such Listener(make sure you call these line after your layout is display else it will give a blank screen). 为您的主布局提供ID并在屏幕上显示内容后,在一些Listener说按钮单击或菜单项或任何此类监听器上写下面的代码(确保在显示布局后调用这些行,否则它将给出一个黑屏)。

    View content = findViewById(R.id.myLayout);
    content.setDrawingCacheEnabled(true);
    getScreen(content);
    method getScreen(content)

    private void getScreen(View content)
        {
            Bitmap bitmap = content.getDrawingCache();
            File file = new File("/sdcard/test.png");
            try 
            {
                file.createNewFile();
                FileOutputStream ostream = new FileOutputStream(file);
                bitmap.compress(CompressFormat.PNG, 100, ostream);
                ostream.close();
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }

Also don't for to add permission for writing file to SDCard. 也不要添加将文件写入SDCard的权限。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
                                                                   </uses-permission>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM