当前位置: 首页 > 知识库问答 >
问题:

我在发送屏幕截图时出错

佴涵蓄
2023-03-14

用户一点击按钮,我就让他截图发给另一个朋友。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
            Bitmap bitmap = getScreenShot(rootView);
            File file = store(bitmap,"File-Name");
            shareImage(file);
        }
    });

public static Bitmap getScreenShot(View view) {
    View screenView = view.getRootView();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
    screenView.setDrawingCacheEnabled(false);
    return bitmap;
}

public static File store(Bitmap bm, String fileName){
    final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
    File dir = new File(dirPath);
    if(!dir.exists())
        dir.mkdirs();
    File file = new File(dirPath, fileName);
    try {
        FileOutputStream fOut = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return file;
}
private void shareImage(File file){
    Uri uri = Uri.fromFile(file);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/*");

    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    try {
        startActivity(Intent.createChooser(intent, "Share Screenshot"));
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show();
    }
}

调试时,我看到位图值是“”。我的错误信息中也有这些。

在此处输入图像说明

我在我的舱单文件里用过。

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

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_path" />
</provider>

你能帮帮我吗?

共有1个答案

常献
2023-03-14

您应该使用以下代码

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
                Bitmap bitmap = getScreenShot(rootView);
                File file = store(bitmap, "name.png");

                if (file.exists()) {
                    shareImage(file);
                } else {
                    Log.e("file exist", "NO");
                }
            }
        });



    public static Bitmap getScreenShot(View view) {
        View screenView = view.getRootView();
        screenView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
        screenView.setDrawingCacheEnabled(false);
        return bitmap;
    }

    public File store(Bitmap bm, String fileName) {
        final String dirPath = getExternalCacheDir().getAbsolutePath() + "/Screenshots";
        File dir = new File(dirPath);
        if (!dir.exists())
            dir.mkdirs();
        File file = new File(dirPath, fileName);
        try {
            FileOutputStream fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return file;
    }

    private void shareImage(File file) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getPath()));
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, "Share Screenshot"));
    }
 类似资料:
  • 在Linux下有很多屏幕载图的工具,下面简单介绍一下: 在GNOME桌面中自带了一个屏幕截图工具,位于“动作”栏内。该工具功能很少,只能截取当前屏幕。 在GMIP中也可截图,在“文件”--“获取”菜单下有一个“屏幕抓图”选项可进行屏幕截图。它可截取任意图窗口的内容,并自动输入到GMIP中,我们可方便地进行处理和保存。 安装ImageMagick软件,它有一个工具叫import可用于屏幕截图。该工具

  • 点击按钮进行截屏,可以将截屏图像保存到相册中。 作者说:听说会和苹果的策略有冲突,应用如果上架可能会被拒绝。这个估计是看人品了吧。经过测试发现,如果先弹出对话框,然后再截屏,似乎并不能把对话框也给保存下来。 [Code4App.com]

  • 问题内容: 我正在尝试获取包含SKScene的视图的屏幕抓图。我使用的技术是: 这对于普通的UIViews效果很好,但是无论出于何种原因,它都忽略了SKScene中的所有精灵。 我不确定这是否是错误,或者Sprite Kit的渲染是否与UIGraphics分开。 问题:当适用于UIViews的方式似乎不适用于Sprite Kit或有人成功将UIGraphics上下文与Sprite Kit结合使用时

  • 我在使用html2canvas。js库,用于与selenium一起拍摄全页屏幕截图。 我保存了html2canvas。我的java项目的类路径中的js文件。我用来获取屏幕截图的java脚本代码是: 我能够捕获flipkart页面的全页屏幕截图,但其中没有任何图像。 我无法使用Chrome的TakeScreenshot实用程序,因为它不允许使用Chrome浏览器拍摄整页屏幕截图。

  • 我正在使用AlarmManager和Pending Intent调用BroadcastReceiver类。这是每天安排的。 以下是“活动”中调用的BroadCast Receiver类的代码(它是一个单独的类)。 问题陈述是,在此接收器中,我正在打开另一个应用程序,我想捕获屏幕截图并将其上传到服务器。但是窗口功能在广播接收器类中不可用,我无法实现,因为我超出了活动控制。 关键挑战: -在以下类中实

  • 我试图根据用户输入的坐标捕捉区域截图。基本上,用户在屏幕上点击得到x,y坐标,然后在其他地方点击另一对x,y坐标,然后将其放入一个矩形中,并使用机器人库创建屏幕截图。 我有的问题是,我得到了随机截图,这不是用户输入的坐标,我怎么能考虑包括0的坐标,因为矩形值必须超过1。 以下是我迄今为止的代码:

  • 原文:Screenshots 这里你会找到一些示例图和生成它们的代码。 简单绘图 这里是一个带有文本标签的基本的绘图: 源代码 子图示例 多个轴域(例如子图)可使用subplot()命令创建: 源代码 直方图 hist()命令自动生成直方图,并返回项数或者概率: 源代码 路径示例 你可以使用matplotlib.path模块,在maplotlib中添加任意路径: 源代码 mplot3d mplot

  • 我正在尝试使用Python 3.6(在Windows 10上)的保存一个网站的屏幕截图。当我使用或函数时,总会出现以下异常: WebDriver异常:未知错误:无法从未知错误中获得自动化扩展:无法找到页面:chrome-扩展://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html (会话信息:chrome=60.0.31