android- 以编程方式截取特定视图的屏幕截图不起作用
android- Taking screenshot of a particular view programmatically does not work
我想截取另一个 activity 的布局。当我查看位图时,它显示空指针异常。这是我的代码
View v=LayoutInflater.from(this).inflate(R.layout.score_share, null);
layoutScore.setDrawingCacheEnabled(true);
Bitmap bm= Bitmap.createBitmap(v.getDrawingCache());
layoutScore.setDrawingCacheEnabled(false);
File file= new File(Environment.getExternalStorageDirectory()+"/scs.jpg");
try {
FileOutputStream outputStream= new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG,100, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
如果问题是 getDrawingCache 返回 null,那么只需在 v.buildDrawingCache(true);
之前添加这行代码
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
这将防止视图的大小为 (0,0),因此它变成空安全的。
下面是截图方法。
public Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
将Bitmap存储到SDCard中:
public void store(Bitmap bm, String fileName){
final static String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dirPath);
if(!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
现在扩展您的布局并捕获屏幕截图
View view=LayoutInflater.from(this).inflate(R.layout.score_share, null);
Bitmap bmp = getScreenShot(view);
bmp 是所需的屏幕截图,然后将其保存到 SD 卡中,如
store(bmp, "Screenshot.jpg");
不要忘记在清单中添加写入外部存储权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
感谢这个回答
我想截取另一个 activity 的布局。当我查看位图时,它显示空指针异常。这是我的代码
View v=LayoutInflater.from(this).inflate(R.layout.score_share, null);
layoutScore.setDrawingCacheEnabled(true);
Bitmap bm= Bitmap.createBitmap(v.getDrawingCache());
layoutScore.setDrawingCacheEnabled(false);
File file= new File(Environment.getExternalStorageDirectory()+"/scs.jpg");
try {
FileOutputStream outputStream= new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG,100, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
如果问题是 getDrawingCache 返回 null,那么只需在 v.buildDrawingCache(true);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
这将防止视图的大小为 (0,0),因此它变成空安全的。
下面是截图方法。
public Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
将Bitmap存储到SDCard中:
public void store(Bitmap bm, String fileName){
final static String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dirPath);
if(!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
现在扩展您的布局并捕获屏幕截图
View view=LayoutInflater.from(this).inflate(R.layout.score_share, null);
Bitmap bmp = getScreenShot(view);
bmp 是所需的屏幕截图,然后将其保存到 SD 卡中,如
store(bmp, "Screenshot.jpg");
不要忘记在清单中添加写入外部存储权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
感谢这个回答