Android takes a screenshot

Hi, I tried to write a function that takes a screenshot. I found code that does this perfectly with the clicklistner button, but when I remove the clicklistner button and just try to take a screenshot in oncreate, the bitmap that I get is empty. Why is this happening?

location:

<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/LinearLayout01" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="munch" android:id="@+id/munchscreen" /> <ImageView android:layout_width="fill_parent" android:layout_height="500dp" android:id="@+id/screenshots" /> 

Activity:

 public class MainActivity extends Activity { LinearLayout L1; ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); L1 = (LinearLayout) findViewById(R.id.LinearLayout01); Button but = (Button) findViewById(R.id.munchscreen); but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View v1 = L1.getRootView(); v1.setDrawingCacheEnabled(true); Bitmap bm = v1.getDrawingCache(); BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); image = (ImageView) findViewById(R.id.screenshots); image.setBackgroundDrawable(bitmapDrawable); } }); } } 
+5
source share
2 answers

just try to take a screen shot in oncreate the bitmap i get is empty. Why is this happening?

The user interface will not be displayed yet. Given the approach you are using, you need the infrastructure to really draw the user interface before you can take a screenshot.

If instead you have root View draw() on a Bitmap -backed Canvas , which may already work in onCreate() . It depends on whether there was another root View with measure() and layout() , and I'm not sure if this is happening already on onCreate() or at a later point.

+3
source

you can try this. the public class MainActivity extends the action {LinearLayout L1; Image ImageView;

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); L1 = (LinearLayout) findViewById(R.id.LinearLayout01); Button but = (Button) findViewById(R.id.munchscreen); /*but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View v1 = L1.getRootView(); v1.setDrawingCacheEnabled(true); Bitmap bm = v1.getDrawingCache(); BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); image = (ImageView) findViewById(R.id.screenshots); image.setBackgroundDrawable(bitmapDrawable); } });*/ } @Override public void onPostCreate() { View v1 = L1.getRootView(); v1.setDrawingCacheEnabled(true); Bitmap bm = v1.getDrawingCache(); BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); image = (ImageView) findViewById(R.id.screenshots); image.setBackgroundDrawable(bitmapDrawable); } } 
0
source

Source: https://habr.com/ru/post/1238521/


All Articles