Android 从活动中截图

Android 从活动中截图,android,Android,我试图使用此代码从活动中截取一个屏幕截图,但我在代码行中得到一个java.lang.nullpointerexception: Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight) 当我试着调试时,我发现所有的变量都无法解析;i、 例如,高度、宽度、高度。请帮我解决这个问题 Activity av; View view; @Ove

我试图使用此代码从活动中截取一个屏幕截图,但我在代码行中得到一个java.lang.nullpointerexception:

Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight)
当我试着调试时,我发现所有的变量都无法解析;i、 例如,高度、宽度、高度。请帮我解决这个问题

Activity av; 
View view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    view = (View)findViewById(R.id.screen);
    av= HelloAndroidActivity.this;

    try {   
    takeScreenShot(av);
    }
    catch (Exception e) 
    { }
}  

public void takeScreenShot(Activity av) throws Exception 
{ 
    view =this.getWindow().getDecorView();
    //or
    //view =av.getWindow().getDecorView();

    view.setDrawingCacheEnabled(true); 
    view.buildDrawingCache(true);           

    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = this.getWindowManager().getDefaultDisplay().getWidth();
    int height = this.getWindowManager().getDefaultDisplay().getHeight();

    Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();

    FileOutputStream fos = null; 

    try 
    { 
        //File sddir = new File(path, name);
        File root = Environment.getExternalStorageDirectory();

        //s= sddir.toString();
        Toast.makeText(this, "path " + root, Toast.LENGTH_LONG).show();

        if (!root.exists()) 
        { 
            root.mkdirs(); 
        }               

        fos = new FileOutputStream(root+ "/Image" + "_" + "4" + ".jpeg");              

        if (fos != null) 
        { 
            b.compress(Bitmap.CompressFormat.JPEG, 90, fos); 
            fos.flush();
            fos.close();                  
        } 
    } 
    catch (Exception e) 
    { 
        Toast.makeText(this, "Exception " + e, Toast.LENGTH_LONG).show();
        Log.e("Exception occurred while moving image: ",  e.toString());
        e.printStackTrace();
    } 
} 

使用以下代码避免出现null位图:



仔细看看,您必须使用:

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

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

我使用了以下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>


结果是:

谢谢你的帮助,但这件事我已经知道了。我想知道的是如何从一个活动中获得活动截图。请帮我查一下相关代码
<?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>