【问题标题】:Getting a black screenshot获取黑色屏幕截图
【发布时间】:2018-01-03 21:55:08
【问题描述】:

基本上,我想截取整个滚动视图的屏幕截图。我尝试了很多方法,但找不到完美的方法。

我尝试了以下方法:

public void takeScreenShot() {
    mbitmap = getBitmapOFRootView();
    createImage(mbitmap);
}

public void createImage(Bitmap bmp) {

    String path = Environment.getExternalStorageDirectory().toString() + "/screenshot.jpg";
    try {
        FileOutputStream outputStream = new FileOutputStream(new File(path));
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public Bitmap getBitmapOFRootView() {
    mScrollView.setDrawingCacheEnabled(true);
    int totalHeight = mScrollView.getChildAt(0).getHeight();
    int totalWidth = mScrollView.getChildAt(0).getWidth();
    mScrollView.layout(0,0, totalWidth, totalHeight);
    mScrollView.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(mScrollView.getDrawingCache());
    mScrollView.setDrawingCacheEnabled(false);
    return b;
}

这种方法几乎可以工作,但它只显示了 2 个视图和一个按钮,除了整个屏幕是黑色的:

我的 xml 包含这么多视图,它的视图层次结构是这样的:

<ScrollView>
   <ConstraintLayout>
     <Views>
      ....
     <Views>
   </ConstraintLayout>
</ScrollView>

我已经参考了很多 StackOverflow 帖子,但都没有用。 那么有人可以帮我解决吗?

更新: 终于找到了解决办法。因此,这是背景的问题,通过在其上绘制画布解决了它。如下:

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);

    return bitmap;

【问题讨论】:

  • 你想用它来实现自动化吗?如果是这样,您的最佳选择可能是:developer.android.com/reference/android/app/….
  • 不行,我只是想把ScrollView的截图保存到本地。
  • 好的,您的问题可能与您仅从根视图而不是子视图获取位图这一事实有关。您必须获取所有位图,然后正确定位它们。我知道以编程方式截取屏幕截图的唯一其他方法是使用 UIDevice/UIAutomation,但这些应该用于测试,而不是在生产应用程序中。

标签: android scrollview screenshot android-bitmap


【解决方案1】:

你应该使用相同的画布

public static Bitmap saveBitmapFromView(View view, int width, int height) {
 Bitmap bmp = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);                
 Canvas canvas = new Canvas(bmp);
 view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
 view.draw(canvas);
 return bmp;
}

【讨论】:

  • 我也试过了!不工作!得到与上面相同的输出!
【解决方案2】:

以编程方式在 android 中截取和共享屏幕截图

我到处搜索,但发现这个工作正常

takeAndShareScreenshot()

    private void takeAndShareScreenshot(){
        Bitmap ss = takeScreenshot();
        saveBitmap(ss);
        shareIt();
    }

takeScreenshot()

    private Bitmap takeScreenshot() {
        View view = // decore view of the activity/fragment;
        view.setDrawingCacheEnabled(true);
        return view.getDrawingCache();
    }

saveBitmap()

private void saveBitmap(Bitmap bitmap) {
// path to store screenshot and name of the file
        imagePath = new File(requireContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/" + "name_of_file" + ".jpg");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }

shareIt()

private void shareIt() {
        try {
            Uri uri = Uri.fromFile(imagePath);
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("image/*");
            String shareBody = getString(R.string.share_body_text);
            sharingIntent.putExtra(Intent.EXTRA_SUBJECT, R.string.subject);
            sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
            sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);

            startActivity(Intent.createChooser(sharingIntent, "Share via"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

注意:

In recent versions of android (>Marshmallow I guess), you may need `write 
access to external directory`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多