我正在使用此代码截取截图:
public voID saveScreenshot() { if (ensureSDCardAccess()) { Bitmap bitmap = Bitmap.createBitmap(getWIDth(),getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); onDraw(canvas); file file = new file(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg"); fileOutputStream fos; try { fos = new fileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); fos.close(); } catch (fileNotFoundException e) { Log.e("Panel","fileNotFoundException",e); } catch (IOException e) { Log.e("Panel","IOEception",e); } } } /** * Helper method to ensure that the given path exists. * Todo: check external storage state */ private boolean ensureSDCardAccess() { file file = new file(mScreenshotPath); if (file.exists()) { return true; } else if (file.mkdirs()) { return true; } return false; }
但是,当运行以下行时:
Bitmap bitmap = Bitmap.createBitmap(getWIDth(),Bitmap.Config.ARGB_8888);
我的应用程序关闭时出现以下异常:
11-28 15:05:46.291: E/AndroIDRuntime(8209): java.lang.IllegalArgumentException: wIDth and height must be > 0
如果我更改高度和宽度,屏幕截图被采取,但它是空的:
为什么会这样?我究竟做错了什么?
解决方法 你可以这样做,给你的主要布局&在屏幕上显示内容之后,在一些监听器上写下面的代码,说按钮单击或菜单项或任何这样的监听器(确保在布局显示后调用这些行,否则将给出一个空白屏幕).
VIEw content = findVIEwByID(R.ID.myLayout); content.setDrawingCacheEnabled(true); getScreen(content);
方法getScreen(content)
private voID getScreen(VIEw content) { Bitmap bitmap = content.getDrawingCache(); file file = new file("/sdcard/test.png"); try { file.createNewfile(); fileOutputStream ostream = new fileOutputStream(file); bitmap.compress(CompressFormat.PNG,ostream); ostream.close(); } catch (Exception e) { e.printstacktrace(); } }
也不要添加向SDCard写入文件的权限.
<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission>总结
以上是内存溢出为你收集整理的android – 截图全部内容,希望文章能够帮你解决android – 截图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)