如何将LinearLayout转换为图像?

Adh*_*ham 10 android bitmap android-linearlayout

我尝试了以下代码将转换LinearLayout为图像:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout lyt = (LinearLayout) findViewById(R.id.lyt);
    lyt.setDrawingCacheEnabled(true);
    lyt.buildDrawingCache(true);

    Bitmap b = Bitmap.createBitmap(lyt.getDrawingCache());

    ImageView img = (ImageView) findViewById(R.id.imageView1);
    img.setImageBitmap(b);

}
Run Code Online (Sandbox Code Playgroud)

但我NullPointerException进去了:

Bitmap b = Bitmap.createBitmap(lyt.getDrawingCache());
Run Code Online (Sandbox Code Playgroud)

布局XML是:

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


    <LinearLayout
        android:id="@+id/lyt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3" />

    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

san*_*ram 12

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)