如何在android studio中传递整个活动
问题描述:
我正在开发一个android应用程序,并试图使下面的代码有效。我想要做的是:截取整个活动的截图,包括未显示的文字(必须向上或向下滚动)。 这是截图方法:如何在android studio中传递整个活动
public static Bitmap takeScreenshot(Activity activity){
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
Bitmap bmap = view.getDrawingCache();
Rect statusBar = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);
view.setDrawingCacheEnabled(false);
return snapshot;
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
我想打电话给takeScreenshot以下功能,但我不知道如何在takeScreenshot的参数传递的活动。我试图复制活动的名称,但它不起作用。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed__info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
id = intent.getStringExtra(Urls.MARKER_ID);
year = intent.getStringExtra(Urls.MARKER_Year);
info = intent.getStringExtra(Urls.MARKER_Info);
editTextName = (TextView) findViewById(R.id.markerYear);
editTextDesg = (TextView) findViewById(R.id.detailedInfo);
editTextName.setText(year);
editTextDesg.setText(info);
//Saving into picture
findViewById(R.id.download).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot(); // here's where i have to pass the activity
saveBitmap(bitmap);
}
});
}
答
传递活动与您传递当前活动的上下文相同。您只需要在您的活动中执行以下操作:
takeScreenshot(this)
答
只需传递方法中的活动上下文即可。
Bitmap bitmap = takeScreenshot(MyActivity.this);
会为你工作。
更新
public static Bitmap takeScreenshot(Activity activity) {
try {
// create bitmap screen capture
View v1 = activity.getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
return bitmap;
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
return null;
}
答
试试这个捕捉截图:
public static Bitmap captureScreen(View v) {
Bitmap screenshot = null;
try {
if(v!=null) {
screenshot = Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(screenshot);
v.draw(canvas);
}
}catch (Exception e){
Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());
}
return screenshot;
}
public static void saveImage(Bitmap bitmap) throws IOException{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}
更多参考访问以下链接:
http://karanbalkar.com/2014/03/get-screenshot-of-device-screen-in-android/
+0
这是否捕获整个屏幕,包括未显示的文本(必须向上或向下滚动)? – bhk33
'this'将代表没有活动在这种情况下,因为是这样'onClick' – Pragnani
在这种情况下,这将引用您的活动。 takeScreenshot(Activity.this)将活动替换为您的活动名称。 –
我不是OP,我已经纠正你的答案..我试图纠正你,而不是给downvote – Pragnani