Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the following code to avoid a <strong>null</strong> bitmap:</p> <hr> <pre><code>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); }; } </code></pre> <hr> <p>Take a good look, you have to use:</p> <pre><code> view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); </code></pre> <hr> <p>I used the following <strong>main.xml</strong>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /&gt; &lt;ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <hr> <h2>The outcome is:</h2> <p><img src="https://i.stack.imgur.com/xBMQQ.png" alt="enter image description here"> <a href="https://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null">Reference</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload