Android通过代码截图

标签 android sms screenshot

这应该不是一个太难的问题。我希望能够截取我的布局( View )并通过短信发送。有人可以帮我走几步吗?

谢谢!

编辑: 我猜它不一定是“屏幕截图”,只要我们能以某种方式从 View 中获取所有渲染像素即可。

最佳答案

我在网上找到了一些我能够一起工作的代码 fragment 。

这是一个很好的解决方案:

设置你的根布局:

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

获取渲染 View 的函数:

private void getScreen()
{
    View content = findViewById(R.id.layoutroot);
    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();
    }
}

关于Android通过代码截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5939987/

相关文章:

android - 用于消息访问的 Gmail 内容提供程序

java - LibGDX 屏幕截图奇怪的行为

ios - 如何在 Swift 中截取全屏屏幕截图?

android - fragment 内的 MapFragment,应用程序在重新打开 fragment 时崩溃

web-services - 通过短信调用Web服务API

找不到 Android 工作室 R.id.****

ios - Firebase 身份验证 iOS 12 无效 token

java - 如何在 Windows 中使用 JNA 截屏?

android - moveCamera 和 animateCamera 第二次不起作用

android - 在 android 中使用 upi ://进行 UPI 支付的回调 url