应用截图?
App Screenshot?
我有一个 Google 地图 Android 应用程序,我试图将整个视图的屏幕截图与地图一起捕获。已截取屏幕截图,但 Google 地图全黑,只有 Google 徽标。我使用以下代码:
private void captureScreen() {
View v = getWindow().getDecorView().getRootView();
v.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
try {
FileOutputStream fos = new FileOutputStream(new File(Environment
.getExternalStorageDirectory().toString(), "SCREEN"
+ System.currentTimeMillis() + ".png"));
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mapLayout">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
tools:context="net.aratos.adc.MapActivity"
android:layout_height="0dp"
android:layout_weight="0.8"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.1"
android:layout_gravity="center_horizontal">
<Button
android:gravity="center"
android:id="@+id/loadDateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load date" />
<Button
android:gravity="center"
android:id="@+id/snapshotButton"
android:text="Snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:gravity="center"
android:id="@+id/exportButton"
android:text="Export PDF"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<GridView
android:orientation="horizontal"
android:id="@+id/boundsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="auto_fit">
</GridView>
</LinearLayout>
我也附上布局的代码。
输出截图如下
image
尝试 Brijesh 的代码后,我得到以下结果 output
您可以使用GoogleMap.snapshot方法拍摄地图快照(documentation)
您需要实施
管理拍摄的快照。
此外,请注意,根据文档:
Note:
Images of the map must not be transmitted to your servers, or
otherwise used outside of the application. If you need to send a map
to another application or user, send data that allows them to
reconstruct the map for the new user instead of a snapshot.
以下代码会对您有所帮助
googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap bitmap) {
try {
FileOutputStream fos = new FileOutputStream(new File(Environment
.getExternalStorageDirectory().toString(), "SCREEN"
+ System.currentTimeMillis() + ".png"));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
捕获整个屏幕
尝试关注
googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
try {
getWindow().getDecorView().findViewById(android.R.id.content).setDrawingCacheEnabled(true);
Bitmap backBitmap = getWindow().getDecorView().findViewById(android.R.id.content).getDrawingCache();
Bitmap bmOverlay = Bitmap.createBitmap(
backBitmap.getWidth(), backBitmap.getHeight(),
backBitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(snapshot, new Matrix(), null);
canvas.drawBitmap(backBitmap, 0, 0, null);
try {
FileOutputStream fos = new FileOutputStream(new File(Environment
.getExternalStorageDirectory().toString(), "SCREEN"
+ System.currentTimeMillis() + ".png"));
// Write the string to the file
bmOverlay.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.d("ImageCapture", "FileNotFoundException");
Log.d("ImageCapture", e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("ImageCapture", "IOException");
Log.d("ImageCapture", e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
public void captureMapScreen() {
SnapshotReadyCallback callback = new SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
try {
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()
+ "/MapScreenShot"
+ System.currentTimeMillis() + ".png");
bmOverlay.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
};
mMap.snapshot(callback);
}
确保使用 android.graphics.Matrix 而不是 android.opengl.Matrix
希望对您有所帮助。
我有一个 Google 地图 Android 应用程序,我试图将整个视图的屏幕截图与地图一起捕获。已截取屏幕截图,但 Google 地图全黑,只有 Google 徽标。我使用以下代码:
private void captureScreen() {
View v = getWindow().getDecorView().getRootView();
v.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
try {
FileOutputStream fos = new FileOutputStream(new File(Environment
.getExternalStorageDirectory().toString(), "SCREEN"
+ System.currentTimeMillis() + ".png"));
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mapLayout">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
tools:context="net.aratos.adc.MapActivity"
android:layout_height="0dp"
android:layout_weight="0.8"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.1"
android:layout_gravity="center_horizontal">
<Button
android:gravity="center"
android:id="@+id/loadDateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load date" />
<Button
android:gravity="center"
android:id="@+id/snapshotButton"
android:text="Snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:gravity="center"
android:id="@+id/exportButton"
android:text="Export PDF"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<GridView
android:orientation="horizontal"
android:id="@+id/boundsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="auto_fit">
</GridView>
</LinearLayout>
我也附上布局的代码。 输出截图如下 image
尝试 Brijesh 的代码后,我得到以下结果 output
您可以使用GoogleMap.snapshot方法拍摄地图快照(documentation)
您需要实施
管理拍摄的快照。
此外,请注意,根据文档:
Note: Images of the map must not be transmitted to your servers, or otherwise used outside of the application. If you need to send a map to another application or user, send data that allows them to reconstruct the map for the new user instead of a snapshot.
以下代码会对您有所帮助
googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap bitmap) {
try {
FileOutputStream fos = new FileOutputStream(new File(Environment
.getExternalStorageDirectory().toString(), "SCREEN"
+ System.currentTimeMillis() + ".png"));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
捕获整个屏幕 尝试关注
googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
try {
getWindow().getDecorView().findViewById(android.R.id.content).setDrawingCacheEnabled(true);
Bitmap backBitmap = getWindow().getDecorView().findViewById(android.R.id.content).getDrawingCache();
Bitmap bmOverlay = Bitmap.createBitmap(
backBitmap.getWidth(), backBitmap.getHeight(),
backBitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(snapshot, new Matrix(), null);
canvas.drawBitmap(backBitmap, 0, 0, null);
try {
FileOutputStream fos = new FileOutputStream(new File(Environment
.getExternalStorageDirectory().toString(), "SCREEN"
+ System.currentTimeMillis() + ".png"));
// Write the string to the file
bmOverlay.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.d("ImageCapture", "FileNotFoundException");
Log.d("ImageCapture", e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("ImageCapture", "IOException");
Log.d("ImageCapture", e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
public void captureMapScreen() {
SnapshotReadyCallback callback = new SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
try {
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()
+ "/MapScreenShot"
+ System.currentTimeMillis() + ".png");
bmOverlay.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
};
mMap.snapshot(callback);
}
确保使用 android.graphics.Matrix 而不是 android.opengl.Matrix
希望对您有所帮助。