在android中以编程方式拍摄surfaceview的屏幕截图
我有一个项目,它使用在android中以编程方式拍摄surfaceview的屏幕截图,android,screenshot,surfaceview,Android,Screenshot,Surfaceview,我有一个项目,它使用SurfaceView在屏幕上绘制一些东西。用户应该能够通过按下屏幕上的按钮拍摄屏幕截图。问题是,当从SurfaceView截图时,我总是得到一个空白屏幕。我尝试了很多不同的答案,但是没有一个对我有效。以下是我尝试过的: void takeScre() { File file = saveBitmap(getBitmap()); Intent intent = new Intent(); intent.setAction(Int
SurfaceView
在屏幕上绘制一些东西。用户应该能够通过按下屏幕上的按钮拍摄屏幕截图。问题是,当从SurfaceView
截图时,我总是得到一个空白屏幕。我尝试了很多不同的答案,但是没有一个对我有效。以下是我尝试过的:
void takeScre() {
File file = saveBitmap(getBitmap());
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(intent);
}
Bitmap getBitmap() {
dots_screen_view.setDrawingCacheEnabled(true);
return dots_screen_view.getDrawingCache();
}
//----------
//-----------
我还尝试了这些库:
然而,它们都不适合我。还有其他想法吗?试试。。。我以前用它来创建社交媒体帖子,没有任何问题。它的源代码是开放的,所以如果它可以工作,您可以看到它工作的原因:)请尝试以下代码:
/**
* Check for permissions and create a snapshot
*
* @param activity Activity used by Selfie.
* @return {@code true} if the screenshot was taken, false otherwise.
*/
public boolean snap(Activity activity) {
boolean hasPermission = (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_STORAGE);
return false;
} else {
return takeScreenShot(activity);
}
}
/**
* This method is responsible for taking the screenshot and creating a file
*
* @param activity Activity used by Selfie.
* @return {@code true} if the screenshot was taken, false otherwise.
*/
private boolean takeScreenShot(Activity activity) {
Date now = new Date();
android.text.format.DateFormat.format(fileFormat, now);
// create bitmap screen capture
View v1 = activity.getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
// image naming and path to include sd card appending name you choose for file
String path = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
File imageFile = new File(path);
try {
FileOutputStream outputStream = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException ex) {
return false;
}
return true;
}
请在清单中添加写入权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
早些时候,我也处于类似的十字路口,为了拍摄自定义视图类的屏幕截图,我不得不进行大量的试运行 请尝试以下代码
public String getBitmapFromView() {
int dwidth,dheight;
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
if(displayMetrics.widthPixels > displayMetrics.heightPixels)
{
//orientation is landscape
dwidth=displayMetrics.heightPixels;
dheight=displayMetrics.widthPixels;
}
else
{
dwidth = displayMetrics.widthPixels;
dheight=displayMetrics.heightPixels;
}
setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//If the view wasnt displayed before this , you will get 0,0 measured widths and heights
//Thus measure the layout
measure(MeasureSpec.makeMeasureSpec(dwidth, MeasureSpec.AT_MOST),MeasureSpec.makeMeasureSpec(dheight, MeasureSpec.AT_MOST));
layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
//Create a bitmap backed Canvas to draw the into
Bitmap bitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
if (getMeasuredWidth() == 0 && getMeasuredHeight() == 0) {
//You will get blank bitmap
return "";
} else {
Canvas c = new Canvas(bitmap);
//Now that the view is here and we have a canvas, ask the to draw itself into the canvas
draw(c);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, stream);
byte[] byteArray = stream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
}
在我的场景中,我需要将位图转换为字符串,但是,您可以将其转换为字节,直接使用位图,使用OutputStream将其直接写入文件。您遇到了什么异常?我没有遇到任何异常,但文件的输出为空(其中曲面视图)我来看看这个不,这对我不起作用。在surfaceview元素所在的空白屏幕上:(您是否检查了SD卡内部?正在保存哪个图像
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
public String getBitmapFromView() {
int dwidth,dheight;
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
if(displayMetrics.widthPixels > displayMetrics.heightPixels)
{
//orientation is landscape
dwidth=displayMetrics.heightPixels;
dheight=displayMetrics.widthPixels;
}
else
{
dwidth = displayMetrics.widthPixels;
dheight=displayMetrics.heightPixels;
}
setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//If the view wasnt displayed before this , you will get 0,0 measured widths and heights
//Thus measure the layout
measure(MeasureSpec.makeMeasureSpec(dwidth, MeasureSpec.AT_MOST),MeasureSpec.makeMeasureSpec(dheight, MeasureSpec.AT_MOST));
layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
//Create a bitmap backed Canvas to draw the into
Bitmap bitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
if (getMeasuredWidth() == 0 && getMeasuredHeight() == 0) {
//You will get blank bitmap
return "";
} else {
Canvas c = new Canvas(bitmap);
//Now that the view is here and we have a canvas, ask the to draw itself into the canvas
draw(c);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, stream);
byte[] byteArray = stream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
}