Taking a screenshot of current Activity in Android
来源:互联网 发布:淘宝申诉ps小票 编辑:程序博客网 时间:2024/05/16 23:58
http://androidresearch.wordpress.com/2013/01/06/taking-a-screenshot-of-current-activity-in-android/
In this post I’ll show how you can take a screenshot of your current Activity and save the resulting image on /sdcard.
The idea behind taking a screenshot actually is pretty simple: what we need to do is to get a reference of the root view and generate a bitmap copy of this view.
Considering that we want to take the screenshot when a button is clicked, the code will look like this:
findViewById(R.id.button1).setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
First of all we should retrieve the topmost view in the current view hierarchy, then enable the drawing cache, and after that call getDrawingCache().
Calling getDrawingCache(); will return the bitmap representing the view or null if cache is disabled, that’s why setDrawingCacheEnabled(true); should be set to true prior invoking getDrawingCache().
public
Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(
true
);
return
rootView.getDrawingCache();
}
And the method that saves the bitmap image to external storage:
public
void
saveBitmap(Bitmap bitmap) {
File imagePath =
new
File(Environment.getExternalStorageDirectory() +
"/screenshot.png"
);
FileOutputStream fos;
try
{
fos =
new
FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG,
100
, fos);
fos.flush();
fos.close();
}
catch
(FileNotFoundException e) {
Log.e(
"GREC"
, e.getMessage(), e);
}
catch
(IOException e) {
Log.e(
"GREC"
, e.getMessage(), e);
}
}
Since the image is saved on external storage, the WRITE_EXTERNAL_STORAGE permission should be added AndroidManifest to file:
<
uses-permission
android:name
=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
- Taking a screenshot of current Activity in Android
- Taking a screenshot in XNA 4.0
- Current Screenshot of my Operating System
- Methods of taking a snapshot
- Taking a screenshot from within a Silverlight #WP7 application from http://geekswithblogs.net
- Taking advantage of Observables in Angular 2
- Live in Office (Taking a shower Cont')
- uiautomatorviewer中的Error taking device screenshot错误
- android screenshot
- Taking a Bite Out of ASP.NET ViewState
- screenshot in robotium
- a method to get a screenshot of a WinCE 5.0 device
- A Summary of Current Machine Learning Frameworks
- Taking a shower
- 59.In your database, the current status of a PL/SQL procedure has become INVALID. Which action woul
- How to take screenshot (thumbnail) of a web site with ASP.NET 2.0?
- Android 4.0 截屏(Screenshot)
- Android ScreenShot 屏幕截图
- Adidas NAeW ggW MubG5
- DirectX 9.0c游戏开发手记之“龙书”第二版学习笔记之1: 开场白
- <leetcode> Inorder, Preorder Traversal
- 【MacOS】Mac OS中显示和隐藏文件
- ubuntu 安装心得(UEFI win8)
- Taking a screenshot of current Activity in Android
- 队列
- 寒江独钓 串口过滤 记录
- .htaccess另类用法之蜘蛛判断及域名重定向
- 使用jacob框架_word转pdf
- 搭建wordpress
- 获取当前URL
- sdfgsdfsdfs
- Leetcode: Single Number III (Single Number扩展)