我一直在研究如何在android中以编程方式截取屏幕截图,但是当它截图时,我会获得一个工具栏和黑屏,而不是屏幕上实际显示的内容.
我还尝试在为谷歌地图创建的自定义InfoWindow布局中截取特定的TextVIEw.但是这会在下面的第二行创建一个空指针异常.
TextVIEw v1 = (TextVIEw)findVIEwByID(R.ID.tv_code);v1.setDrawingCacheEnabled(true);
无论如何要么实际屏幕截图屏幕上没有安装androID截图库或截取自定义InfoWindow布局中的TextVIEw
这是我的截图方法:
/** * Method to take a screenshot programmatically */private voID takeScreenshot(){ try { //TextVIEw I Could screenshot instead of the whole screen: //TextVIEw v1 = (TextVIEw)findVIEwByID(R.ID.tv_code); VIEw v1 = getwindow().getDecorVIEw().getRootVIEw(); v1.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); v1.setDrawingCacheEnabled(false); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes); file f = new file(Environment.getExternalStorageDirectory() + file.separator + "test.jpg"); fileOutputStream fo = new fileOutputStream(f); fo.write(bytes.toByteArray()); fo.flush(); fo.close(); MediaStore.Images.Media.insertimage(getContentResolver(), f.getabsolutePath(), f.getname(), f.getname()); Log.d("deBUG", "Screenshot saved to gallery"); Toast.makeText(HuntActivity.this,"Code Saved!",Toast.LENGTH_LONG).show(); } catch (fileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); }}
编辑:我已将方法更改为源提供的方法
How can i take/merge screen shot of Google map v2 and layout of xml both programmatically?
然而,它没有截图任何东西.
public voID captureMapScreen() { GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() { @OverrIDe public voID onSnapshotReady(Bitmap snapshot) { try { VIEw mVIEw = getwindow().getDecorVIEw().getRootVIEw(); mVIEw.setDrawingCacheEnabled(true); Bitmap backBitmap = mVIEw.getDrawingCache(); Bitmap bmOverlay = Bitmap.createBitmap( backBitmap.getWIDth(), backBitmap.getHeight(), backBitmap.getConfig()); Canvas canvas = new Canvas(bmOverlay); canvas.drawBitmap(backBitmap, 0, 0, null); canvas.drawBitmap(snapshot, new Matrix(), null); fileOutputStream out = new fileOutputStream( Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".jpg"); bmOverlay.compress(Bitmap.CompressFormat.JPEG, 90, out); } catch (Exception e) { e.printstacktrace(); } } }; mMap.snapshot(callback);}
解决方法:
我已经弄清楚了!
/** * Method to take a screenshot programmatically */private voID takeScreenshot(){ GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() { @OverrIDe public voID onSnapshotReady(Bitmap bitmap) { Bitmap b = bitmap; String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss", Locale.getDefault()) .format(new java.util.Date()); String filepath = timeStamp + ".jpg"; try{ OutputStream fout = null; fout = openfileOutput(filepath,MODE_WORLD_READABLE); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout); fout.flush(); fout.close(); } catch (fileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } saveImage(filepath); } }; mMap.snapshot(callback);}/** * Method to save the screenshot image * @param filePath the file path */public voID saveImage(String filePath){ file file = this.getfileStreamPath(filePath); if(!filePath.equals("")) { final ContentValues values = new ContentValues(2); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); values.put(MediaStore.Images.Media.DATA, file.getabsolutePath()); final Uri contentUrifile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Toast.makeText(HuntActivity.this,"Code Saved to files!",Toast.LENGTH_LONG).show(); } else { System.out.println("ERROR"); }}
我已经调整了此链接中的代码,因此它不会共享,而只是保存图像.
Capture screen shot of GoogleMap Android API V2
感谢大家的帮助
总结以上是内存溢出为你收集整理的java – 截图Android中的黑色全部内容,希望文章能够帮你解决java – 截图Android中的黑色所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)