0

我正在尝试使用浮动小部件截取屏幕截图。但我找不到任何这样做的方法。我搜索了 MediaProjection API,但找不到任何有用的东西。现在,如果我点击浮动小部件,它只会捕获浮动小部件的屏幕截图。

 captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Screenshot.java is my class for taking the screenshot
            Bitmap bitmap = Screenshot.takescreenshotOfView(v);
            OutputStream fOut = null;
            Uri outputFileUri;
            try {
                File root = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "DUOProfile" + File.separator);
                root.mkdirs();
                File sdImageMainDirectory = new File(root, "profilepic"+".jpg");
                Toast.makeText(getApplicationContext(), "Picture saved in DUOProfile folder",Toast.LENGTH_SHORT).show();
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                fOut = new FileOutputStream(sdImageMainDirectory);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error occured. Please try again later.",Toast.LENGTH_SHORT).show();
            }
            try {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
            } catch (Exception e) {
            }
        }
    });

截图.java

package com;

import android.graphics.Bitmap;
import android.view.View;

public class Screenshot  {

    public static Bitmap takescreenshot(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }

    public static Bitmap takescreenshotOfView(View view) {
        return takescreenshot(view.getRootView());
    }

}
4

1 回答 1

-2

在 takescreenshotOfView 方法中传递视图

Bitmap bitmap = takescreenshotOfView(view);

这将返回位图图像,刚刚保存在您的内部/sd 卡中

 public Bitmap takescreenshotOfView(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
于 2018-07-09T12:59:53.960 回答