如何在android studio中传递整个活动

时间:2016-02-26 11:55:13

标签: java android android-activity

我正在开发一个Android应用程序,我试图使以下代码工作。我想要做的是:截取整个活动的屏幕截图,包括未显示的文本(必须向上或向下滚动)。 这是截图方法:

   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);
        }
    });
}

3 个答案:

答案 0 :(得分:0)

传递活动与传递当前活动的上下文相同。您只需要在您的活动中进行此操作:

takeScreenshot(this)

答案 1 :(得分:0)

只需传递方法中的活动上下文。

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;
}

答案 2 :(得分:0)

尝试使用此功能捕获屏幕截图:

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/