提问者:小点点

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


我需要截取活动的屏幕截图(没有标题栏,用户不应看到屏幕截图实际上已被拍摄),然后通过操作菜单按钮“共享”共享它。我已经尝试了一些解决方案,但它们对我不起作用。有什么想法吗?


共3个答案

匿名用户

我就是这样截屏分享的。

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

View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

其次,捕获根视图:

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

第三,将位图存储到 SD 中:

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();
    }
}

最后分享当前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();
    }
}

我希望你能从我的代码中得到启发。

更新:

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

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

因为它创建和访问外部存储中的文件。

更新:

从Android 7.0开始,Nougat共享文件链接被禁止。要解决这个问题,您必须实现FileProvider并共享“content://”uri not“file://”uri。

这里有一个很好的描述如何去做。

匿名用户

单击监听器创建共享按钮

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

添加两个方法

    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);
    }
}

共享屏幕截图。在此处共享实现

    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"));
    }

匿名用户

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

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"));
}

像这样调用此方法

share(screenShot(view));

通过使用此直接共享屏幕截图而无需阅读