Before Screen capture the screen looks something like this :
After Screen capture the screen shows the preview as :
package com.mayursharma.example.capture.screen;
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.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView imageViewCapture, imageViewPreview;
Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageViewPreview = (ImageView) findViewById(R.id.ImageViewPreview);
imageViewCapture = (ImageView) findViewById(R.id.ImageViewCapture);
imageViewCapture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bitmap = getBitmapOfView(imageViewCapture);
imageViewPreview.setImageBitmap(bitmap);
createImageFromBitmap(bitmap);
}
});
}//onCreate Ends
/*
* @author : Mayur Sharma
* This method is used to create the bitmap of the current activity
* This method accepts any child view of the current view
* You can even pass the parent container like RelativeLayout or LinearLayout as a param
* @param : View v
*/
public Bitmap getBitmapOfView(View v)
{
View rootview = v.getRootView();
rootview.setDrawingCacheEnabled(true);
Bitmap bmp = rootview.getDrawingCache();
return bmp;
}
/*
* @author : Mayur Sharma
* This method is used to create an image file using the bitmap
* This method accepts an object of Bitmap class
* Currently we are passing the bitmap of the root view of current activity
* The image file will be created by the name capturedscreen.jpg
* @param : Bitmap bmp
*/
public void createImageFromBitmap(Bitmap bmp)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File file = new File( Environment.getExternalStorageDirectory() +
"/capturedscreen.jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
ostream.write(bytes.toByteArray());
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}//Class Ends
Create a new xml file in res/drawable folder so as to use it as a border for Preview ImageView.
Put the below source code in the xml file and save it by name "pic_preview_border.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#99ffffff"
android:endColor="#99ffffff"
android:centerColor="#00000000"
android:angle="90" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
<corners android:radius="5dp" />
<stroke
android:width="2dp"
android:color="#ccffffff"
/>
</shape>
Create a new XML file in the res/layout folder as "main.xml" which will be used as a layout for the above mentioned activity. Put the below code in the main.xml file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#0080AA"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:layout_width="fill_parent" android:layout_above="@+id/ImageViewPreview" android:layout_height="wrap_content" android:text="Preview : " /> <ImageView android:layout_width="200dp" android:layout_height="250dp" android:adjustViewBounds="true" android:scaleType="fitXY" android:layout_centerInParent="true" android:id="@+id/ImageViewPreview" android:src="@drawable/user_pic" android:background="@drawable/pic_preview_border" /> <ImageView android:layout_width="130dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="fitXY" android:layout_marginBottom="3dp" android:src="@drawable/capture" android:id="@+id/ImageViewCapture" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Click HERE to Download Complete Source Code.
Click HERE to download the apk.

