【问题标题】:How to take screen-shot of app screen in android?如何在android中截取应用程序屏幕?
【发布时间】:2014-04-10 05:34:16
【问题描述】:

我正在开发一个应用程序,我必须在其中截取应用程序屏幕的屏幕截图 现在我使用下面的代码它不起作用。我是空位图图像

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream  os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/"+ "bs_score_img" +".png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();

String screenShot =  Environment.getExternalStorageDirectory().toString()+"/bs_score_img.png";
Log.i("TAG:Score: screenShot path=", screenShot);
Bitmap bmp = BitmapFactory.decodeFile(new File(screenShot).getAbsolutePath());

【问题讨论】:

标签: android


【解决方案1】:

试试这个:

Process sh = Runtime.getRuntime().exec("su", null,null);

                    OutputStream  os = sh.getOutputStream();
                    os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                    os.flush();

                    os.close();
                    sh.waitFor();

then read img.png as bitmap and convert it jpg as follows 

Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+         
File.separator +"img.png");

//my code for saving
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    screen.compress(Bitmap.CompressFormat.JPEG, 15, bytes);

//you can create a new file name "test.jpg" in sdcard folder.

File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
            f.createNewFile();
//write the bytes in file
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
// remember close de FileOutput

    fo.close();

谷歌有一个库,你可以用它在不root的情况下截图,我试过了,但我相信它会尽快耗尽内存。

试试http://code.google.com/p/android-screenshot-library/

或者试试这个:

View v1 = L1.getRootView();
                    v1.setDrawingCacheEnabled(true);
                    Bitmap bm = v1.getDrawingCache();
                    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
                    image = (ImageView) findViewById(R.id.screenshots);
                    image.setBackgroundDrawable(bitmapDrawable);

【讨论】:

  • 希望下一次,最好贴出测试过的东西,这段代码不行!
【解决方案2】:

我从另一个post找到了这段代码:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

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

【讨论】:

    【解决方案3】:

    假设你点击了按钮

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

    之后你需要这两个方法

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

    【讨论】:

      【解决方案4】:
      public Bitmap screenShot(View view) {
          Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                  view.getHeight(), Config.ARGB_8888);
          Canvas canvas = new Canvas(bitmap);
          view.draw(canvas);
          return bitmap;
      }
      

      只需使用您想要快照的视图调用此方法。因此,如果您想要整个屏幕,只需传入最顶层的 ViewGroup。如果您想要系统控件也只需调用:

      【讨论】:

        【解决方案5】:

        你可以试试这个

           private Bitmap getScreen(){    
            mDecorView = getWindow().getDecorView();
            runOnUiThread(new Runnable() {
            public void run() {
            mDecorView.invalidate();
            mDecorView.post(this);
            }
            });
            View v1 = mDecorView.getRootView();
            System.out.println("Root View : "+v1);
            v1.setDrawingCacheEnabled(true);
            return v1.getDrawingCache();
         }   
        

        这里 mDecorView 是 View 。

        【讨论】:

          【解决方案6】:

          这里您可以如何捕获屏幕并将其保存在您的存储中

          授予在外部存储中创建文件的权限

          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
          

          这是活动代码。

          private void takeScreenshot() {
          Date now = new Date();
          android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
          
          try {
              // image naming and path  to include sd card  appending name you choose for file
              String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
          
              // 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();
          }
          }
          

          这就是捕获屏幕的方式。

          【讨论】:

            猜你喜欢
            • 2011-03-18
            • 2013-11-26
            • 2011-05-08
            • 2022-10-08
            • 2013-11-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多