简体   繁体   中英

how to capture the phone screen programmatically in android

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?

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:

在此处输入图片说明 Reference

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM