Screen Capture in android

前端 未结 4 547
时光说笑
时光说笑 2020-11-27 19:35

I want to develop an application that will take screenshot of android screen..does any one know how to do it..? which is similar to koushik duttas screen shot..But with out

4条回答
  •  误落风尘
    2020-11-27 20:04

    Let's say that you clicked on a button:

    findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
           Bitmap bitmap = takeScreenshot();
           saveBitmap(bitmap);
       }
    });
    

    After that you need these two methods:

    public Bitmap takeScreenshot() {
       View rootView = findViewById(android.R.id.content).getRootView();
       rootView.setDrawingCacheEnabled(true);
       return rootView.getDrawingCache();
    }
    
     public void saveBitmap(Bitmap bitmap) {
        File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }
    

提交回复
热议问题