-2

According to this link(How to programmatically take a screenshot in Android?), I used the answer to take a capture and save it on my device's external memory.

I have all the permission cleared, and according to the log, the file is not empty at all. But I can't find the file neither on the gallery nor the file explorer.

Why is this happening? Can someone help me out with this?

Community
  • 1
  • 1
March3April4
  • 2,131
  • 1
  • 18
  • 37
  • According to that code (`String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";`), you're neither saving it into the gallery nor onto the **external** storage. Don't be fooled by the name of this method: [getExternalStorageDirectory()](https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()). – Phantômaxx Jul 31 '16 at 08:57
  • But, the codes below seems to put the captured image to a file with that directory. Won't that save it to that directory? – March3April4 Jul 31 '16 at 09:48
  • Not onto the **external** storage as you mean it. Please read the official docs, linked in my previos comment. – Phantômaxx Jul 31 '16 at 09:50

1 Answers1

1

On regards to Rotwang, I've figured out what the problem was.

The path of the getExternalStorageDirectory() did not seen to return a valid directory for the file to be saved.

In my case, it was about saving screenshots for the user to look on the gallery.

So, I used Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); instead.

So, the full code for saving a screen capture is below.

 private void takeScreenshot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {

        File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File file = new File(path, now + ".jpg");

        path.mkdirs();

        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(file);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        MediaScannerConnection.scanFile(this,
                new String[] { file.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.e("ExternalStorage", "Scanned " + path + ":");
                        Log.e("ExternalStorage", "-> uri=" + uri);
                    }
                });


    } catch (Throwable e) {
        Log.e("Error", "Exception on TakeScreenshot", e);
    }
}

Thanks again Rotwang.

March3April4
  • 2,131
  • 1
  • 18
  • 37