Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getDrawingCache() always returning null

I want to capture The content of ImageView by using DrawingCache. I written the below code.

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

But I am getting only one image on screen. Later I came to know myScreenshot is null

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

I thought any permissions we have to add in manifest ? or root permission required to achieve this ? Please help me regarding this problem.

like image 631
Yugandhar Babu Avatar asked Jan 20 '12 10:01

Yugandhar Babu


3 Answers

Try calling buildDrawingCache() before getDrawingCache()

EDIT: Call getDrawingCache(), after the page have loaded, instead of onCreate

like image 65
aqs Avatar answered Nov 16 '22 01:11

aqs


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

like image 31
Richa Avatar answered Nov 16 '22 01:11

Richa


You din't do iv1.buildDrawingCache(true); add line before
Bitmap myScreenshot = iv1.getDrawingCache();

like image 1
Mady Cool Avatar answered Nov 16 '22 00:11

Mady Cool