Showing posts with label snapshot. Show all posts
Showing posts with label snapshot. Show all posts

Tuesday, 22 October 2013

On Launch

Take Snapshot of Application (Android)

On Take Snap

Take Snapshot of Application (Android)

Create new Android Project
Project Name: SnapApplication
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: SnapApplication
Package Name: com.shaikhhamadali.blogspot.snapapplication
Create layout file: activity_take_snap

  1. create layout:


<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"
    tools:context=".TakeSnap"
    android:id="@+id/rlTakeSnap"
    android:background="#123456" >

    <Button
        android:id="@+id/btnTakeSnap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:text="Take Snap"
         />

    <ImageView
        android:id="@+id/imVShowSnap"
        android:layout_width="match_parent"
        android:layout_height="380dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="#000000"
        android:padding="5dp"
        android:src="@drawable/sample_image" />

</RelativeLayout>

  2. code of activity:


package com.shaikhhamadali.snapapplication;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class TakeSnap extends Activity {
 //variables
 Button btnTakeSnap;
 ImageView imVShowSnap;
 RelativeLayout rlTakeSnap;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_take_snap);
  //create instance of image view and button 
  btnTakeSnap=(Button)findViewById(R.id.btnTakeSnap);
  imVShowSnap=(ImageView)findViewById(R.id.imVShowSnap);
  //on button click take snapshot of application screen
  btnTakeSnap.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    //create instance of Layout
    rlTakeSnap=(RelativeLayout)findViewById(R.id.rlTakeSnap);
    //Enable drawing cache to get bitmap from this view on calling getDrawingCache()
    rlTakeSnap.setDrawingCacheEnabled(true);
    //get bitmap from layout cached drawing and show in image view
    imVShowSnap.setImageBitmap(rlTakeSnap.getDrawingCache());
   }
  });

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.take_snap, menu);
  return true;
 }

}

This works on all devices but even then if you get null always then try this method:

public static Bitmap loadBitmapFromView(View v) {
     //get view width and height
     Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888); 
     //create canvas instance and pass bitmap               
     Canvas c = new Canvas(b);
     v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
     //Draw Canvas on View
     v.draw(c);
     //return Generate bitmap
     return b;
}

  3. note that:

  • It is necessary to setDrawingCacheEnabled(true) either you will get null in getDrawingCache().
  • you can clear drawing cache by rlTakeSnap.setDrawingCacheEnabled(false);
  • Learn about get GetDrawingCache

  4. conclusion:

  • Some information about how to snapshot of Application.
  • know what is Drawing Cache and how to get image as bitmap from view.

  5. About the post:


  • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

  6. Source Code:
        you can download the source code here