如何使用代码拍摄屏幕快照,而不仅仅是 Android 中的应用程序
How to take snapshot of screen not only app in Android with code
我开发了可以截图的应用程序。
但它只拍摄应用程序的快照。我想从应用程序中获取快照。
我已经研究了答案,但我还没有找到答案。
这是我的代码。
View view = getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
saveImageToAppFolder(bitmap);
saveImagetoAppFolder 是将图像保存到应用程序文件夹的功能。
那不是问题。
有没有办法截屏?
我不知道你在 saveImageToAppFolder 中的代码是什么,但你可以试试这个:
注意:您需要将 app/activity 的背景设置为透明 (100%)。
//your code below is extractly
View view = getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
//try my code for save image file to storage
File imgFile = new File(imgPath);
FileOutputStream os = new FileOutputStream(imageFile);
int imgQuality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, imgQuality , os);
os.flush();
os.close();
设置透明背景的代码:
//首先:为透明
创建下面的主题xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
这样设置后:
<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
</activity>
注意:您可以从此处查看更多详细信息 url:How do I create a transparent Activity on Android?
要获取设备屏幕的屏幕截图,仅当您有 root 权限时
像这样调用 screencap 二进制文件:
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + Environment.getExternalStorageDirectory()+ "/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor()
并将该文件加载到位图中,使用
public static Bitmap decodeSampledBitmapFromFile(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
我开发了可以截图的应用程序。 但它只拍摄应用程序的快照。我想从应用程序中获取快照。 我已经研究了答案,但我还没有找到答案。 这是我的代码。
View view = getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
saveImageToAppFolder(bitmap);
saveImagetoAppFolder 是将图像保存到应用程序文件夹的功能。 那不是问题。 有没有办法截屏?
我不知道你在 saveImageToAppFolder 中的代码是什么,但你可以试试这个:
注意:您需要将 app/activity 的背景设置为透明 (100%)。
//your code below is extractly
View view = getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
//try my code for save image file to storage
File imgFile = new File(imgPath);
FileOutputStream os = new FileOutputStream(imageFile);
int imgQuality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, imgQuality , os);
os.flush();
os.close();
设置透明背景的代码:
//首先:为透明
创建下面的主题xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
这样设置后:
<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
</activity>
注意:您可以从此处查看更多详细信息 url:How do I create a transparent Activity on Android?
要获取设备屏幕的屏幕截图,仅当您有 root 权限时 像这样调用 screencap 二进制文件:
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + Environment.getExternalStorageDirectory()+ "/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor()
并将该文件加载到位图中,使用
public static Bitmap decodeSampledBitmapFromFile(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}