3

I would like to write a code to take a screenshot of the list view even if it is not visible in the screen fully. Can anyone help me in this issue.

Here's is my code and when I try to run this the app crashes, it says that there is null pointer exception in this line //int totalHeight = listView.getChildAt(0).getHeight(); int totalWidth = listView.getChildAt(0).getWidth(); Please help me resolve this issue.

Button screenShot;
public String TAG = "";
String[] values = new String[] { "1", "2", "3", "4", "5", "6", "7", "8",
        "9", "10", "11", "12", "13", "14", "15" };
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TAG = getClass().getName();

    screenShot = (Button) findViewById(R.id.screenshot_button);
    listView = (ListView) findViewById(R.id.listView);
    int totalHeight = listView.getChildAt(0).getHeight();
    int totalWidth = listView.getChildAt(0).getWidth();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);
    listView.setAdapter(adapter);
    listView.layout(0, 0, totalWidth, totalHeight);



    screenShot.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "Inside on click",
                    Toast.LENGTH_SHORT).show();
            Bitmap bitmap = takeScreenshot();
            saveBitmap(bitmap);
        }
    });
}

public Bitmap takeScreenshot() {
    Log.d(TAG, "Inside takeScreenShot method");
    listView.buildDrawingCache(true);
     Bitmap b = Bitmap.createBitmap(listView.getDrawingCache());    
    // View rootView = findViewById(android.R.id.content).getRootView();
    // rootView.setDrawingCacheEnabled(true);
    return listView.getDrawingCache();  

}

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory()
            + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

}

Parthiban M
  • 1,104
  • 1
  • 10
  • 30

1 Answers1

1

This is because when onCreate is called your view hasn't been drawn yet so you can't get it's height. Try to add this code at the end of your onCreate method to wait for the ListView to be drawn and get the height:

listView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        // You only need this method to be called once
        listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        // Store the height in a class variable
        mItemHeight = listView.getChildAt(0).getHeight()
    }
});

EDIT: I missed .getViewTreeObserver()

peguerosdc
  • 946
  • 11
  • 21
  • Thanks for the answer, when I tried adding this block it throws a error saying, "The method addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){}) is undefined for the type ListView". Can you please help me solving it...!!! – Parthiban M Nov 18 '14 at 09:22
  • Sorry, I missed a method, see my edited answer please :) – peguerosdc Nov 18 '14 at 09:42
  • Thanks for your consideration, however when I tried running my code the screenshot is only with the items which are visible on the screen only. But not the entire list view. – Parthiban M Nov 18 '14 at 12:59
  • This is because my code snippet is only to get the height of a row, you need to mix my answer with what Usman told you. Please try it by yourself and in a couple of hours when I get to my computer I will try to help you :) – peguerosdc Nov 18 '14 at 15:59
  • Hi, I've posted my entire code with all the changes what you and Usman have advised. Please look at the following link and help me fixing this issue. http://stackoverflow.com/questions/26996847/trying-to-take-screenshot-of-a-entire-listview – Parthiban M Nov 19 '14 at 05:23