Android take screenshot

In your application, you might want to allow the user to take screenshot of the application's interface. In this Android, I am going to show you how to take screenshot programmatically and save the captured image in the external storage directory of Android phone.
To achieve this goal, first you need to use the getRootView() to get the most top level view or root view. Then call the getDrawingCache() method of the root view to get the captured image. The image data is stored in a Bitmap object. You can use the compress(Bitmap.CompressFormat format, int quality, OutputStream stream) method of Bitmap to output the compressed image in JPEG, or PNG format to an ByteArrayOutputStream object that later can be written to your external storage using FileOutputStream class.
Now, to have an example application on taking screenshot, you create a new Android project. Then add one Button and ImageView to the activity_main.xml file as shown below.


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
<Button
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Capture screen"
   android:onClick="takeScreenshot"
   />
<ScrollView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   >
<ImageView
   android:id="@+id/imgView"
   android:layout_width="300dp"  
   android:layout_height="400dp"
  
   />  
</ScrollView>
</LinearLayout>


In the MainActivity class, you need to add two methods: takeScreenshort() and saveImage(). The takeScreenshot() method is invoked when the user clicks the Take Screenshot button to capture the screen and save the image (by calling the saveImage() method) to the external storage. You have to add the external storage write-permission to AndroidManifest.xml file to allow the application to save the image in the external storage.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Here is the content of the MainActivity class.
package com.example.andexamples;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends FragmentActivity {
ImageView imgView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView=(ImageView)findViewById(R.id.imgView);
}

public void takeScreenshot(View view){
// Get root View of your application
View rootView = findViewById(android.R.id.content).getRootView();
// Enable drawing cache
rootView.setDrawingCacheEnabled(true);
// Create image
Bitmap bitmap = rootView.getDrawingCache();  
// Show image in ImageView
imgView.setImageBitmap(bitmap);  
// Save image in external storage
saveImage(bitmap);


}

public void saveImage(Bitmap bitmap) {  
try {  
// Create ByteArrayOutputStream object to store bytes of compressed image
ByteArrayOutputStream bytes = new ByteArrayOutputStream();      
// Compress the image in jpeg format
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
// Create FileOutputStream object to write image to external storage    
FileOutputStream fo = new FileOutputStream(Environment.getExternalStorageDirectory()+ File.separator + "screenimg.jpg");      
fo.write(bytes.toByteArray());
// Close output file
fo.close();  
}
catch (FileNotFoundException e) {      
Log.e("Main", e.getMessage());  
} catch (IOException e) {
Log.e("MAain", e.getMessage(), e);  
}
}
}

Android-take-screenshot

Posted by: Dara | post date: 10-18-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.