Archive

Posts Tagged ‘activity’

Taking a screenshot of current Activity in Android

January 6, 2013 15 comments

In this post I’ll show how you can take a screenshot of your current Activity and save the resulting image on /sdcard.

The idea behind taking a screenshot actually is pretty simple: what we need to do is to get a reference of the root view andĀ  generate a bitmap copy of this view.

screenshot

Considering that we want to take the screenshot when a button is clicked, the code will look like this:

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
       Bitmap bitmap = takeScreenshot();
       saveBitmap(bitmap);
   }
});

 

First of all we should retrieve the topmost view in the current view hierarchy, then enable the drawing cache, and after that call getDrawingCache().

Calling getDrawingCache(); will return the bitmap representing the view or null if cache is disabled, that’s why setDrawingCacheEnabled(true); should be set to true prior invokingĀ  getDrawingCache().

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   return rootView.getDrawingCache();
}

 

And the method that saves the bitmap image to external storage:

 public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

 

Since the image is saved on external storage, the WRITE_EXTERNAL_STORAGE permission should be added AndroidManifest to file:

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

Defining Global Variables in Android

March 22, 2012 17 comments

At some point in your development process you may need to have several variables across application.

In this post I want to describe 2 ways through you can define global variables in Android: using a singleton class, and by extending the Android’s Application class.

1. Using a Singleton class

One of the simplest ways (it does not mean that is one of the best) to have global variables is to declare a singleton class:

public class Globals{
   private static Globals instance;

   // Global variable
   private int data;

   // Restrict the constructor from being instantiated
   private Globals(){}

   public void setData(int d){
     this.data=d;
   }
   public int getData(){
     return this.data;
   }

   public static synchronized Globals getInstance(){
     if(instance==null){
       instance=new Globals();
     }
     return instance;
   }
}

To use this class you get an instance first, and then do what you want with data:

Globals g = Globals.getInstance();
g.setData(100);

....
int data=g.getData();

Notice that we do not instantiate the object by calling “new”, in fact this wouldn’t be allowed as we declared the constructor private, so its not visible. Instead we call the getInstance() method and return our static object.

2. By Extending the Application class

The second way to define global variables is by extending the Application class. This is the base class for maintaining global application state.

a) Create a new class that extends Application.

public class Globals extends Application{
   private int data=200;

   public int getData(){
     return this.data;
   }

   public void setData(int d){
     this.data=d;
   }
}

b) Add the class to the AndroidManifest file as an attribute of <application> tag:

<application
   android:name=".Globals"
   .... />

c) Then you can access your global data from any Activity by calling getApplication()

Globals g = (Globals)getApplication();
int data=g.getData();

 

I said that using the Singleton pattern it’s one of the simplest ways to define global variables, but I mentioned also that it does not mean that it’s one of the best ways to do it. In fact this is quite a controversial subject.

You may believe that when a static variable is initialized it stays so for the entire life of the application, however this does not seem to be true everytime. Sometimes, there are situations when some static variables bound to activities happens to be uninitialized even they have been initialized.

This makes the static singleton approach not one of the best for keeping global variables, though the official Android documentation seems to encourage using them: “There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way.”

Follow

Get every new post delivered to your Inbox.

Join 509 other followers

%d bloggers like this: