1

I want to know how to capture phone screen programmatically. I just googled and got to know that I have to use view.getDrawingCache(). But getDrawingCache() always returns null.

My xml layout is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/screenlayout">
<ImageView  
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/screen"
/>
</LinearLayout>

java file::

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View vScreen = findViewById(R.id.screenlayout);
    ImageView imageview = (ImageView)findViewById(R.id.screen);

    Bitmap bitmap;
    View v1 = vScreen.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());   //getting null here     
    v1.setDrawingCacheEnabled(false)      
    imageview.setImageBitmap(bitmap);

}

Can someone please correct me where I am going wrong?

pavan
  • 1,085
  • 4
  • 22
  • 34

2 Answers2

1

Use the following code to avoid a null bitmap:


public class AndroidWebImage extends Activity {

ImageView bmImage; 
LinearLayout view;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      view = (LinearLayout)findViewById(R.id.screen);
      bmImage = (ImageView)findViewById(R.id.image);

      view.setDrawingCacheEnabled(true);
      // this is the important code :)  
      // Without it the view will have a dimension of 0,0 and the bitmap will be null          

      view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

      view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

      view.buildDrawingCache(true);
      Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
      view.setDrawingCacheEnabled(false); // clear drawing cache

      bmImage.setImageBitmap(b);   

};


}

Take a good look, you have to use:

 view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

I used the following main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
   android:id="@+id/screen"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<ImageView
  android:id="@+id/image"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
/>
</LinearLayout>

The outcome is:

enter image description here Reference

Community
  • 1
  • 1
Imran Rana
  • 11,899
  • 7
  • 45
  • 51
  • I tried the way u suggested but still no luck. view.getDrawingCache() is returning null. – pavan May 19 '12 at 12:01
  • Did you tried with my sample xml or with your's one? If it is your xml then trey adding a TextView/Button with some text like `` and see what happens. – Imran Rana May 19 '12 at 19:51
  • With your xml it worked. But I want to show the captured image but it is showing the text alone. I mean when I launch the app it should take the picture of all the icons of the phone screen .Can you please help me achieve this? Thanks! – pavan May 20 '12 at 12:47
  • It is not showing the text alone, it is showing the captured sceen(It is showing text because I only attached a TextView with text), If you add other objects like buttons or pictures to my xml, it will also show them as a captured image of the activity screen @pavan – Imran Rana May 20 '12 at 14:53
  • Sorry for not providing complete information. I do not want to capture activity, I want to capture window, like "steamy window" application.I have to capture the current screen and show when I launch the app. – pavan May 20 '12 at 15:49
0

All the programs which allow screenshots work only on rooted phones? if your phone is a rooted one you can try using this :

public class ScreenCapture extends Activity{

        LinearLayout view;

        @Override

        protected void onCreate(Bundle savedInstanceState) {

                // TODO Auto-generated method stub

                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);



                view = (LinearLayout)findViewById(R.id.screen);

                Button myBtn = (Button)findViewById(R.id.myBtn);



                myBtn.setOnClickListener(new View.OnClickListener(){

                        @Override

                        public void onClick(View v) {

                                // TODO Auto-generated method stub

                                View v1 = view.getRootView();

                                System.out.println("Root View : "+v1);

                                v1.setDrawingCacheEnabled(true);

                                Bitmap bm = v1.getDrawingCache();



                                System.out.println("Bitmap : "+bm);

                                showScreen(bm);

                        }

                });



        }

}

You can also see this Library: Android Screenshot Library (ASL) enables to programmatically capture screenshots from Android devices without requirement of having root access privilege

K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • It doesn't require root permission to capture screen of your own application IMHO, at least, I'm using it to capture activity screen in non-rooted emulator. – Imran Rana May 18 '12 at 17:03
  • When I launch my app it should capture that screen and should show it.On top of it I want to display another image. So what should be the view (layout or imageview) in my case as your code snippet shows it captures on a button click? – pavan May 18 '12 at 17:08
  • you do screen capture with ddms but not with code or im wrong!! – K_Anas May 18 '12 at 17:09
  • the view should be the layout – K_Anas May 18 '12 at 17:10
  • I tried with both(layout and imageview) still getDrawingCache() is returning null. – pavan May 18 '12 at 17:14
  • @pavan I think your problem will be solved, take a look at my answer :) – Imran Rana May 19 '12 at 05:07
  • This just take screenshot of active application only. Not entire android screen. – Nguyen Minh Binh Sep 19 '12 at 08:53