我正在尝试使用浮动小部件截取屏幕截图。但是我找不到任何方法。我搜索了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());
}
}
在 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;
}