以截图 [英] Taking Screenshot

查看:220
本文介绍了以截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的截屏设备中的应用程序。在这个应用中,我们可以在屏幕上画任何东西。为此,我现在用的帆布,油漆和路径要做到这一点。

I'm developing an application for taking screenshots in the device. In this application, we can draw anything on the screen. For this I am using Canvas, Paint and Path to do this.

我用这个code采取截图:

I'm using this code to take screenshots:

        public void saveScreenshot() 
    {
        if (ensureSDCardAccess()) 
        {
            Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            onDraw(canvas);
            File file = new File(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg");
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.e("Panel", "FileNotFoundException", e);
            } catch (IOException e) {
                Log.e("Panel", "IOEception", e);
            }
        }
    }

    /**
     * Helper method to ensure that the given path exists.
     * TODO: check external storage state
     */
    private boolean ensureSDCardAccess() {
        File file = new File(mScreenshotPath);
        if (file.exists()) {
            return true;
        } else if (file.mkdirs()) {
            return true;
        }
        return false;
    }

但是,下面的线运行时:

However, when the following line is run:

Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);

我的应用程序有以下异常关闭:

my application closes with the following exception:

11-28 15:05:46.291: E/AndroidRuntime(8209): java.lang.IllegalArgumentException: width and height must be > 0

如果我改变高度和宽度,截图采取,但它是空的:

If I change the height and width, the screenshot is taken, but it's empty:

这是为什么发生?我究竟做错了什么?

Why is that happening? What am I doing wrong?

推荐答案

您可以像这样,

给这个ID为您的主要布局和放大器;之后,你在屏幕上显示的内容写了一些监听器说,按一下按钮或菜单项或任何这样的监听器(确保你打电话给你的布局后,这些线显示否则会给一个空白屏幕)。<以下code / P>

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).

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

方法getScreen(内容)

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();
        }
    }

也不要为添加权限写入文件的SD卡。

Also don't for to add permission for writing file to SDCard.

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

这篇关于以截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆