截图Android中的黑色(Screenshot Black in Android)
我一直在研究如何在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(); } }
编辑:我已将方法更改为源提供的方法
我如何以编程方式获取/合并谷歌地图v2的屏幕截图和xml的布局?
但它没有截图任何东西。
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); }
I've been working out how to take a screenshot programmatically in android, however when it screenshots I get a toolbar and black screen captured instead of what is actually on the screen.
I've also tried to screenshot a particular TextView within the custom InfoWindow layout I created for the google map. But that creates a null pointer exception on the second line below.
TextView v1 = (TextView)findViewById(R.id.tv_code); v1.setDrawingCacheEnabled(true);
Is there anyway to either actually screenshot what is on the screen without installing android screenshot library or to screenshot a TextView within a custom InfoWindow layout
This is my screenshot method:
/** * 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(); } }
EDIT: I have changed the method to the one provided from the source
How can i take/merge screen shot of Google map v2 and layout of xml both programmatically?
However it does not screenshot anything.
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); }
原文:https://stackoverflow.com/questions/36878574
最满意答案
从.NET 3.5 SP1开始,您可以使用:
<TextBlock Height="50" Text="{Binding Path=MODULE_GUID, StringFormat='Test: {0}'}" />
Starting with .NET 3.5 SP1, you can use:
<TextBlock Height="50" Text="{Binding Path=MODULE_GUID, StringFormat='Test: {0}'}" />
相关问答
更多-
在WPF中自定义文本环绕(Custom Text Wrapping in WPF)[2022-03-17]
不幸的是,没有完全实现一个TextFormatter就没有一个简单的方法。 有关高级TextFormatter的基础知识的MSDN文章 : WPF中的文本布局和UI控件提供了格式属性,使您可以在应用程序中轻松地包含格式化文本。 这些控件提供了许多属性来处理文本的显示,包括字体,大小和颜色。 在一般情况下,这些控件可以处理应用程序中的大部分文本显示。 但是,一些高级场景需要控制文本存储以及文本显示。 WPF为此提供了一个可扩展的文本格式引擎。 Unfortunately there isn't a strai ... -
下面是一个从ContentControl派生完成你想要的完整示例: 在WPF 4中创建自定义的UserControls(派生自ContentControl) Pete的ContentPresenter和你的例子一样。 Here is a complete example of deriving from ContentControl to accomplish what you want: Creating Customized UserControls (Deriving from ContentCont ...
-
以下是我最终做的与MVVM模式一起使用的方法: 我有两套数据绑定在我的视图模型上:一个用于实际网格数据,另一个用于列标题。 目前这些暴露为两个属性: // INotifyPropertyChanged support not shown for brevity public DataTable GridData { get; set; } public BindingList
ColumnData { get; set; } 处理两组不同数据的技巧在网格中。 我已经将 ... -
您将DisplayMemberPath和SelectedValuePath设置为“Name”,因此我假设您有一个具有公共属性名称的PhoneBookEntry类。 您是否将DataContext设置为ConnectionViewModel对象? 我复制了你的代码,做了一些小的修改,似乎工作正常。 我可以设置视图模型PhoneBookEnty属性和组合框中选定的项目的更改,我可以更改组合框中选定的项目,并且视图模型PhoneBookEntry属性设置正确。 这是我的xaml:
与WPF中的自定义文本绑定(Binding with custom text in WPF)[2023-07-10]
从.NET 3.5 SP1开始,您可以使用:Starting with .NET 3.5 SP1, you can use: 绑定自定义属性类WPF(binding a custom property class WPF)[2022-05-29]
尝试两件事public class RFPGegevens { private string _klant; public String Klant { get { return _klant; } set { _klant = value; ... WPF中的自定义控件(Custom Control in WPF)[2022-10-22]
这里有教程如何创建自定义控件 。 [1.]添加名为“ButtonImg”的新项“自定义控件(WPF)”。 完成此步骤后,VS将为您创建两个文件:“ButtonImg.cs”和“/Themes/Generic.xaml”。 [2.]在“ButtonImg.cs”文件中添加几个依赖属性: 我创建了属性:图像源,文本,图像宽度和高度。 public class ButtonImg : Control { static ButtonImg() { DefaultStyleKeyPro ...WPF自定义按钮和绑定(WPF Custom Button and Binding)[2024-02-07]
你不能直接做你想做的事。 实现该功能的方法是将ControlTemplate和Style放入UserControl ,然后添加三个DependencyProperty以绑定到: