Get screenshot of surfaceView in Android -
in application have view buttons , surfaceview. must take screenshot of view, in place of surfaceview black field. try solutions found on stackoverflow, don't work me.
this code 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); } { bitmap.recycle(); view.setdrawingcacheenabled(false); if (fos != null) { try { fos.close(); } catch (ioexception e) { log.e(tag, e.getmessage(), e); } } } maybe can help.
i found solution how make screenshot (but don't know if correct use). in surfaceview created method drawbitmap:
public bitmap drawbitmap() { bitmap bitmap = bitmap.createbitmap(getwidth(), getheight(), bitmap.config.argb_8888); canvas canvas = new canvas(bitmap); thread.dodraw(canvas); return bitmap; } in next step compress bitmap:
fileoutputstream fos = null; try { fos = new fileoutputstream(file); bitmap.compress(compressformat.png, 100, fos); } { bitmap.recycle(); if (fos != null) { try { fos.close(); } catch (ioexception e) { log.e(tag, e.getmessage(), e); } } }
Comments
Post a Comment