如何把应用程序屏幕的屏幕截图在android系统? [英] How to take screen-shot of app screen in android?
本文介绍了如何把应用程序屏幕的屏幕截图在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发中,我不得不采取应用程序屏幕的屏幕截图应用程序
现在我用低于code这是行不通的。我是空的位图图像
过程SH =调用Runtime.getRuntime()EXEC(苏,NULL,NULL);
OutputStream的OS = sh.getOutputStream();
os.write((/系统/斌/撷取画面-p+/ SD卡/+bs_score_img+PNG)的getBytes(ASCII));
os.flush();
os.close();
sh.waitFor();字符串截图= Environment.getExternalStorageDirectory()的toString()+/ bs_score_img.png。
Log.i(TAG:得分:截图路径=截图);
BMP位图= BitmapFactory.de codeFILE(新文件(截图).getAbsolutePath());
解决方案
在这里,您如何捕获屏幕,并保存在您的存储
授予权限创建外部存储文件
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
这是code的活动。
私人无效takeScreenshot(){
现在日期=新的日期();
android.text.format.DateFormat.format(YYYY-MM-dd_hh:MM:SS,如今);尝试{
//图像的命名和路径包括SD卡附加您选择的文件名
字符串的mpath = Environment.getExternalStorageDirectory()的toString()+/+ +现在.JPG。 //创建位图的屏幕截图
查看V1 = getWindow()getDecorView()getRootView()。
v1.setDrawingCacheEnabled(真);
位图的位图= Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(假); 文件镜像文件=新的文件(的mpath); FileOutputStream中的OutputStream =新的FileOutputStream(镜像文件);
INT质量= 100;
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,质量,OutputStream的);
outputStream.flush();
outputStream.close(); openScreenshot(镜像文件);
}赶上(Throwable的E){
//几个错误可能拿出文件处理或OOM
e.printStackTrace();
}
}
这是你如何捕获屏幕。
I am developing an application in which I have to take screen-shot of app screen right now I used below code it is not working. I am null bitmap image
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/"+ "bs_score_img" +".png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
String screenShot = Environment.getExternalStorageDirectory().toString()+"/bs_score_img.png";
Log.i("TAG:Score: screenShot path=", screenShot);
Bitmap bmp = BitmapFactory.decodeFile(new File(screenShot).getAbsolutePath());
解决方案
Here how you can capture screen and save it in your storage
give permissions for creating file in external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
And this is code for activity.
private void takeScreenshot() {
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.getExternalStorageDirectory().toString() + "/" + now + ".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();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
This is how you can capture the screen.
这篇关于如何把应用程序屏幕的屏幕截图在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文