Showing posts with label how to take screenshot. Show all posts
Showing posts with label how to take screenshot. Show all posts

Monday, 3 October 2011

How to take a screen shot programmatically

Here you can See take screen shot programmatically and display screen shot in Image View and and check weather SD card is in your device or not , To save screen shot in to the SD Card.

Xml(main.xml)

In main.xml Declare all view objects.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
android:id="@+id/btn_screenshoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Click Here to Create ScreenShot" />

<ImageView
android:id="@+id/imgv_showscreenshot"
android:layout_width="216dp"
android:layout_height="360dp"
android:layout_below="@+id/btn_screenshoot"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />

</RelativeLayout>

Android Activity(ScreenshotActivity .java)

In this Activity we can see take a screenshot programatically when user click on Button.

package com.androidsurya.screenshot;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class ScreenshotActivity extends Activity {

Button btn_screenshoot;
int i = 0;
ImageView imgv_showscreenshot;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btn_screenshoot = (Button) findViewById(R.id.btn_screenshoot);
btn_screenshoot.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

View view = findViewById(R.id.relativelayout);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
imgv_showscreenshot = (ImageView) findViewById(R.id.imgv_showscreenshot);
// set screenshot bitmapdrawable to imageview
imgv_showscreenshot.setBackgroundDrawable(bitmapDrawable);
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
// we check if external storage is available, otherwise
// display an error message to the user using Toast Message
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()
+ "/ScreenShots");
directory.mkdirs();

String filename = "screenshot" + i + ".jpg";
File yourFile = new File(directory, filename);

while (yourFile.exists()) {
i++;
filename = "screenshot" + i + ".jpg";
yourFile = new File(directory, filename);
}

if (!yourFile.exists()) {
if (directory.canWrite()) {
try {
FileOutputStream out = new FileOutputStream(
yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 90,
out);
out.flush();
out.close();
Toast.makeText(
ScreenshotActivity.this,
"File exported to /sdcard/ScreenShots/screenshot"
+ i + ".jpg",
Toast.LENGTH_SHORT).show();
i++;
} catch (IOException e) {
e.printStackTrace();
}

}
}
} else {
Toast.makeText(ScreenshotActivity.this,
"Sorry SD Card not available in your Device!",
Toast.LENGTH_SHORT).show();
}

}
});

}

}

 AndroidManifest.xml:

 In manifest file to store screenshot in sdcard
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
is mandatory. 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidsurya.screenshot"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidsurya.screenshot.ScreenshotActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Output Screenshot:






















Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...