Note that there are some explanatory texts on larger screens.

plurals
  1. PONullpointerException when trying to save the current Android app view as image
    primarykey
    data
    text
    <p>Hi StackOverflow community,</p> <p>in my application I'd like to have a button which allows the user to capture the current view.</p> <p>For that reason, I've written the following method (oriented towards: <a href="https://stackoverflow.com/questions/3107527/android-save-view-to-jpg-or-png">Android save view to jpg or png</a> ):</p> <pre><code>private LinearLayout container; public void createImageOfCurrentView(View v) { if (isExternalStoragePresent()) { container = (LinearLayout) findViewById(R.id.container); container.setDrawingCacheEnabled(true); Bitmap b = container.getDrawingCache(); File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/" + getPackageName() + "/"); dir.mkdirs(); File file = new File(dir, "image.jpg"); FileOutputStream fos; try { fos = new FileOutputStream(file); b.compress(CompressFormat.JPEG, 95, fos); // NullPointerException! Toast.makeText(this, "The current view has been succesfully saved " + "as image.", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { Toast.makeText(this, "Unfortunatly an error occured and this " + "action failed.", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "Bacause of lacking access to the storage " + "of this device, the current view couldn't be saved as an image.", Toast.LENGTH_LONG).show(); } } </code></pre> <p>The problem is according to <code>LogCat</code> that there occured a <code>NullPointerException</code> when trying to create the <code>jpeg</code>. So probably the method <code>container.getDrawingCache()</code> is not working. On the phone the file is generated. However with the size of 0 bytes and no visible image. I would appreciate any suggestions what I have to do in order to make it work as I want.</p>
    singulars
    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.
 

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