Taking Screenshot

♀尐吖头ヾ 提交于 2019-11-27 20:51:13

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

        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.

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

Exception is because the height and width of Bitmap you are creating is zero

try below code to get height and width

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

In case there is no access to getWindowManager

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

getWidth(), getHeight() required to be called with context, if your are trying it outside Activity it will fail. Try getApplicationContext.getWidth().

Could you show more of your code pls? It seems like you are calling width and height have not positive integer values. You could debug this by printing the values of width and height.

I faced a similar issue and the solution was:

You get width and heigh before your view is drown so check first if width or heigh equal zero:

if (getWidth() == 0 || getHeight() == 0) {
            initRemote(remote);
            isViewInitialized=false;
        }

isViewInitialized is a member value.

Then put this code in OnSizeChanged

@Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        super.onSizeChanged(w, h, oldw, oldh);
        if(!isViewInitialized&&){
            // this is the right place to take snapshot :)
        }

    }

I have wrapped the screenshot code into a very simple library. It allows you to take screenshots and store them to the disk if you want.

https://github.com/abdallahalaraby/Blink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!