使用浮动小部件拍摄任意屏幕的屏幕截图

时间:2018-07-09 12:46:41

标签: java android android-mediaprojection

我正在尝试使用Floating Widget截屏。但是我找不到任何办法。我搜索了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) {
            }
        }
    });

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

}

1 个答案:

答案 0 :(得分:-1)

通过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;
    }