Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the code that allowed my screenshot to be stored on an SD card and used later for whatever your needs are:</p> <p>First, you need to add a proper permission to save the file:</p> <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt; </code></pre> <p>And this is the code (running in an Activity):</p> <pre><code>private void takeScreenshot() { Date now = new Date(); android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); try { // image naming and path to include sd card appending name you choose for file String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; // create bitmap screen capture View v1 = getWindow().getDecorView().getRootView(); v1.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); v1.setDrawingCacheEnabled(false); File imageFile = new File(mPath); FileOutputStream outputStream = new FileOutputStream(imageFile); int quality = 100; bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); outputStream.flush(); outputStream.close(); openScreenshot(imageFile); } catch (Throwable e) { // Several error may come out with file handling or DOM e.printStackTrace(); } } </code></pre> <p>And this is how you can open the recently generated image:</p> <pre><code>private void openScreenshot(File imageFile) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(imageFile); intent.setDataAndType(uri, "image/*"); startActivity(intent); } </code></pre> <p>If you want to use this on fragment view then use:</p> <pre><code>View v1 = getActivity().getWindow().getDecorView().getRootView(); </code></pre> <p>instead of</p> <pre><code>View v1 = getWindow().getDecorView().getRootView(); </code></pre> <p>on <strong>takeScreenshot()</strong> function</p> <p><strong>Note</strong>:</p> <p>This solution doesn't work if your dialog contains a surface view. For details please check the answer to the following question:</p> <p><a href="https://stackoverflow.com/questions/27817577/android-take-screenshot-of-surface-view-shows-black-screen">Android Take Screenshot of Surface View Shows Black Screen</a></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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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