如何截取当前活动的截图然后分享?

jus*_*eko 32 android share screenshot android-studio

我需要截取屏幕截图Activity(没有标题栏,用户不应该看到实际拍摄的截图),然后通过动作菜单按钮"分享"进行分享.我已经尝试了一些解决方案,但它们对我不起作用.有任何想法吗?

Sil*_*ght 88

这就是我捕捉屏幕并分享它的方式.

首先,从当前活动获取根视图:

View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
Run Code Online (Sandbox Code Playgroud)

其次,捕获根视图:

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

,存入BitmapSD卡:

public static void store(Bitmap bm, String fileName){
    final static 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,分享当前的截图Activity:

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(context, "No App Available", Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望你的灵感来自我的代码.

更新:

将以下权限添加到您的AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

因为它创建并访问外部存储中的文件.

更新:

从Android 7.0开始,Nougat共享文件链接是禁止的.要处理这个问题,你必须实现FileProvider并共享"content://"uri而不是"file://"uri.

是一个很好的描述如何做到这一点.

  • java.io.FileNotFoundException:/ storage/emulated/0 /截图/图像:打开失败:ENOENT(没有这样的文件或目录) (4认同)
  • 像魅力一样工作,但如果您想使用此代码,您应该擅长编码! (2认同)

Sar*_*Hon 18

此方法不需要存储和检索屏幕截图以再次共享。只需方法调用,您就能够共享屏幕截图。

private Bitmap screenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

private void share(Bitmap bitmap){
    String pathofBmp=
            MediaStore.Images.Media.insertImage(ctx.getContentResolver(),
                    bitmap,"title", null);
    Uri uri = Uri.parse(pathofBmp);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Star App");
    shareIntent.putExtra(Intent.EXTRA_TEXT, "");
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    ctx.startActivity(Intent.createChooser(shareIntent, "hello hello"));
}
Run Code Online (Sandbox Code Playgroud)

像这样调用这个方法

share(screenShot(view));
Run Code Online (Sandbox Code Playgroud)

通过使用此直接共享屏幕截图而无需读取和写入权限

  • @VLeonovs,如果您可以改进我的答案,而不在设备上存储屏幕截图并请求存储权限,那就太好了。 (2认同)

Man*_*dan 11

单击侦听器创建共享按钮

    share = (Button)findViewById(R.id.share);
    share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap bitmap = takeScreenshot();
            saveBitmap(bitmap);
            shareIt();
        }
    });
Run Code Online (Sandbox Code Playgroud)

添加两种方法

    public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
    }

public void saveBitmap(Bitmap bitmap) {
    imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
Run Code Online (Sandbox Code Playgroud)

分享屏幕截图.在这里分享实施

    private void shareIt() {
    Uri uri = Uri.fromFile(imagePath);
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("image/*");
    String shareBody = "In Tweecher, My highest score with screen shot";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
Run Code Online (Sandbox Code Playgroud)

  • 干净优雅的答案:) (3认同)
  • 这是好编码! (2认同)

Man*_*ish 5

您可以使用以下代码获取您在屏幕上看到的视图的位图 您可以指定要创建位图的视图。

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


Cha*_*rts 5

在我添加之前,我无法得到Silent Knight 的工作答案

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

到我的AndroidManifest.xml.