Take screenshot in android application programmatically and Save into Sd-Card memory

How to capture android mobile phone app screen dynamically on button click and save image inside sd-card gallery.

Every android mobile phone gives us the functionality to get current mobile phone screenshot using volume up, volume down, power button combination keys. But these combinations are very complex because every mobile phone has its own combination. So here is the complete step by step tutorial for Take screenshot in android application programmatically and Save into Sd-Card memory.

android-project-download-code-button

Note: Please copy WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

How to Take screenshot in android application programmatically and Save into Sd-Card memory.

Code for MainActivity.java file.

 package com.takescreenshotprogrammatically_android_examples.com;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {

 Button TakeScreenShot;
 ImageView ScreenShotHold;
 Bitmap bitmap;
 View view;
 ByteArrayOutputStream bytearrayoutputstream;
 File file;
 FileOutputStream fileoutputstream;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 TakeScreenShot = (Button)findViewById(R.id.button1);
 
 ScreenShotHold = (ImageView)findViewById(R.id.imageView);
 
 bytearrayoutputstream = new ByteArrayOutputStream();
 
 TakeScreenShot.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View OnclickView) {
 
 view = OnclickView.getRootView();
 
 view.setDrawingCacheEnabled(true);
 
 bitmap = view.getDrawingCache();
 
 ScreenShotHold.setImageBitmap(bitmap);
 
 bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream);
 file = new File( Environment.getExternalStorageDirectory() + "/screenshot.png");
 try 
 {
 file.createNewFile();
 fileoutputstream = new FileOutputStream(file);
 fileoutputstream.write(bytearrayoutputstream.toByteArray()); 
 fileoutputstream.close();
 } 
 catch (Exception e) 
 {
 e.printStackTrace();
 } 
 }
 });
 }
 }

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.takescreenshotprogrammatically_android_examples.com.MainActivity"
 android:id="@+id/RelativeLayout1" >

 <ImageView
 android:id="@+id/imageView"
 android:layout_width="fill_parent"
 android:layout_height="300dp"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:background="#01fb9a"
 >
 
 </ImageView>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:text="Take screenshot in android application programmatically and Save into Sd-Card mo"
 />

</RelativeLayout>

Screenshots:

screenshot-1

Take screenshot in android application programmatically and Save into Sd-Card mo

Click here to download Take screenshot in android application programmatically and Save into Sd-Card project.

Leave a Reply

Your email address will not be published. Required fields are marked *