5

In my application I have view with some buttons and SurfaceView. I must take screenshot of this view, but in place of SurfaceView is only black field. I try solutions which I found on stackoverflow, but they don't work for me.

This is my code which I usualy take screenshots:

File folder = new File(Environment.getExternalStorageDirectory().getPath() + "/folder");
if (!folder.exists()) {
    folder.mkdir();
}
String path = folder.getAbsolutePath() + "snapshot.png";
File file = new File(path);
file.createNewFile();

View view = this.findViewById(android.R.id.content).getRootView();
view.setDrawingCacheEnabled(true);
Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(drawingCache);

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(file);
    bitmap.compress(CompressFormat.PNG, 100, fos);
} finally {
    bitmap.recycle();
    view.setDrawingCacheEnabled(false);
    if (fos != null) {
        try {
            fos.close();
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }
}

Maybe someone can help.

sajar
  • 81
  • 1
  • 2
  • 6
  • AFAIK, there is no way to take a screenshot of an ordinary `SurfaceView`. – CommonsWare Mar 01 '13 at 14:54
  • But I can take screen capture through DDMS and applications making screenshots. So there should be a way... – sajar Mar 01 '13 at 14:58
  • You will notice that DDMS does not run on the device. There are no "applications making screenshots" of other applications except those that have root permissions or are exploiting security holes in various devices. – CommonsWare Mar 01 '13 at 14:59
  • I have rooted phone. So there is no way to make screenshot... Thanks for answers – sajar Mar 01 '13 at 15:02
  • There Alreay exists an answer here:https://stackoverflow.com/a/16978565/2851915 – sailor May 26 '17 at 03:45

0 Answers0