[英]Capture Screen Android, but not my App
大家好,我會盡量保持清晰.....我有一種方法可以獲取應用程序的屏幕,我真正希望的是能夠使用我的方法從其他應用程序獲取屏幕,或者Android桌面,我嘗試這樣做的方法是在捕獲屏幕之前,將Layout轉換為INVISIBLE,但是如果我捕獲了完美的應用程序,但是我想捕獲其他應用程序,則捕獲變為黑色。有任何想法嗎? ...我告訴你我的方法..
公共無效addListenerOnButton4(){
Button btnTakeScreenshot = (Button) findViewById(R.id.share);
btnTakeScreenshot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takeScreenshot();
}
});
}
public void takeScreenshot() {
RelativeLayout ln = (RelativeLayout) findViewById(R.id.Layout);
ln.setBackgroundColor(Color.TRANSPARENT);
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = (Environment
.getExternalStoragePublicDirectory(Environment
.DIRECTORY_DOWNLOADS) +File.separator+now+"ScreenShoot.jpg");
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
-API等級21+
您可以使用MediaProjectionManager
。 詳細信息在這里。
MediaProjection
實現為您提供了可以保存為JPEG的Bitmap
。 獲得byteArray
只需像通常在Java中那樣將其寫入文件即可。
Bitmap bmp = // your Bitmap here
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
-21之前的API
Android提供了一種僅捕獲View
的方法,但是當您將其設置為INVISIBLE
, View
不再具有顏色。 這就是為什么您會出現黑屏的原因。 您無法拍攝其他應用程序的屏幕截圖,只是使View
不可見,因為捕獲功能已連接到View
。
-任何API + root
如果仍然要執行此操作,則需要root訪問。 然后,您可以讀取framebuffer
並獲取原始數據,並將其轉換為Bitmap
。 在此處查看詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.