如何在一段时间后捕获屏幕?

时间:2016-04-16 10:24:27

标签: android eclipse screenshot

实际上我正在尝试从我的活动中捕获屏幕截图,代码获取当前活动的屏幕截图,但我需要的是代码必须在用户单击按钮一分钟后拍摄屏幕的屏幕截图。

这是我的代码所以:

  @Override
      public void run() {
       try {
        serverSocket = new ServerSocket(SocketServerPORT);
        takeScreenshot();
        for(int i=0;i<5;i++) 
        {
      //  Socket socket = serverSocket.accept();
            takeScreenshot();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

       //   FileTxThread fileTxThread = new FileTxThread(socket);
      //  fileTxThread.start();

        }
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }


    private void takeScreenshot()
{


    try {
        // image naming and path  to include sd card  appending name you choose for file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/sna/" + "screenyash"+i + ".jpg";
        i++;
        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

       // openScreenshot(imageFile);
    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您要创建一个Handler和一个Runnable,如下所示:

    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //Take screenshot();
        }
    };
    final Handler handler = new Handler();

然后在按钮上创建一个clickListener,启动runnable以截取屏幕截图:

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            handler.postDelayed(runnable, ONE_MINUTE);
        }
    });

其中

private final int ONE_MINUTE = 60*1000; //1 minute in milliseconds

希望有所帮助!