java – 截图 – 后台服务
发布时间:2020-12-15 04:39:00 所属栏目:Java 来源:网络整理
导读:我正在尝试使用后台服务截取屏幕截图.这项服务就像一个Facebook聊天室,但我希望它在我点击时截取屏幕截图. Preview Picture 我已经开发了一些代码,但它不起作用.我试过的最后一次是: private void takeScreenshot() { Date now = new Date(); android.text.
我正在尝试使用后台服务截取屏幕截图.这项服务就像一个Facebook聊天室,但我希望它在我点击时截取屏幕截图.
Preview Picture 我已经开发了一些代码,但它不起作用.我试过的最后一次是: private void takeScreenshot() { Date now = new Date(); android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss",now); // image naming and path to include sd card appending name you choose for file String mPath = Environment.getExternalStorageDirectory().toString() + "/capture/" + now + ".jpg"; // create bitmap screen capture Bitmap bitmap; View v1 = chatHead.getRootView(); v1.setDrawingCacheEnabled(true); bitmap = Bitmap.createBitmap(v1.getDrawingCache()); v1.setDrawingCacheEnabled(false); OutputStream fout = null; File imageFile = new File(mPath); try { fout = new FileOutputStream(imageFile); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fout); fout.flush(); fout.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 但是截图到我的按钮而不是屏幕. 我知道问题在这里: View v1 = chatHead.getRootView(); 但我不知道如何解决它.谁能帮我? 我实际上使用的是Android Studio 2.2.2和Android 4.0或更高版本. 解决方法
要获取包含不属于您的应用的视图的屏幕截图,您需要使用MediaProjectionManager.
见How to take a screen shot with status bar contents in android application? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |