Note that there are some explanatory texts on larger screens.

plurals
  1. POtake a screenshot from the activity
    primarykey
    data
    text
    <p>I am trying to use this code to take a screenshot from the activity but I am getting a java.lang.nullpointerexception at a line with the code:</p> <pre><code>Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight) </code></pre> <p>and when I tried to debug I found that all the variables are not able to resolve; i.e., statusBarHeight, width, height. Please help me in resolving this.</p> <pre><code>Activity av; View view; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); view = (View)findViewById(R.id.screen); av= HelloAndroidActivity.this; try { takeScreenShot(av); } catch (Exception e) { } } public void takeScreenShot(Activity av) throws Exception { view =this.getWindow().getDecorView(); //or //view =av.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(true); Bitmap b1 = view.getDrawingCache(); Rect frame = new Rect(); this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = this.getWindowManager().getDefaultDisplay().getWidth(); int height = this.getWindowManager().getDefaultDisplay().getHeight(); Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); FileOutputStream fos = null; try { //File sddir = new File(path, name); File root = Environment.getExternalStorageDirectory(); //s= sddir.toString(); Toast.makeText(this, "path " + root, Toast.LENGTH_LONG).show(); if (!root.exists()) { root.mkdirs(); } fos = new FileOutputStream(root+ "/Image" + "_" + "4" + ".jpeg"); if (fos != null) { b.compress(Bitmap.CompressFormat.JPEG, 90, fos); fos.flush(); fos.close(); } } catch (Exception e) { Toast.makeText(this, "Exception " + e, Toast.LENGTH_LONG).show(); Log.e("Exception occurred while moving image: ", e.toString()); e.printStackTrace(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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