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