GetDrawingCache () always returns null

I want to capture the contents of ImageView using DrawingCache. I wrote the code below.

iv1 = (ImageView)findViewById(R.id.iv1); iv2 = (ImageView)findViewById(R.id.iv2); iv1.setDrawingCacheEnabled(true); Bitmap myScreenshot = iv1.getDrawingCache(); iv2.setImageBitmap(myScreenshot); 

But I get only one image on the screen. Later I found out that myScreenshot is null

I saw many posts regarding the same problem, but no correct solution.

I was thinking of any permissions that we should add in the manifest? or root privileges needed to achieve this? Please help me in solving this problem.

+6
source share
4 answers

Try calling buildDrawingCache() to getDrawingCache()

EDIT: Call getDrawingCache() after loading the page instead of onCreate

+10
source

call getDrawingCache () in onWindowFocusChanged () and not in onCreate (), then you will not get zero.

+2
source

You do not want iv1.buildDrawingCache(true) ; add a line to Bitmap myScreenshot = iv1.getDrawingCache();

+1
source
 imageview.setBackgroundResource(R.drawable.imageview); imageview1.setBackgroundResource(R.drawable.imageview1); 

use these two screan images

  <RelativeLayout android:id="@+id/item" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/image" android:layout_width="320dp" android:layout_height="486dp" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:background="@drawable/iv1" /> <ImageView android:id="@+id/ic_launcer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/iv2" /> </RelativeLayout> 
0
source

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


All Articles