简体   繁体   English

如何在Android中拍摄应用程序屏幕的屏幕截图?

[英]How to take screen-shot of app screen in android?

I am developing an application in which I have to take screen-shot of app screen right now I used below code it is not working. 我正在开发一个应用程序,我必须立即拍摄应用程序屏幕的屏幕截图我使用下面的代码它不起作用。 I am null bitmap image 我是null位图图像

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

try this : 尝试这个 :

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

Google has a library with which you can take screenshot without rooting, I tried that, But i am sure that it will eat out the memory as soon as possible. 谷歌有一个图书馆,你可以在没有生根的情况下截取屏幕截图,我试过了,但我相信它会尽快消耗内存。

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

or try this : 或试试这个:

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

I found this code from another 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();
}

Let's say that you clicked on button 假设你点击了按钮

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

After that you need this 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.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);
    }
}
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;
}

just call this method with the view you want a snapshot of.. so if you want the whole screen just pass in you top most ViewGroup. 只需使用您想要快照的视图调用此方法..所以如果您希望整个屏幕只传入最顶层的ViewGroup。 if you want the System controls also just call: 如果你想要系统控件也只是调用:

you can try this 你可以试试这个

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

here mDecorView is View . 这里mDecorView是View。

Here how you can capture screen and save it in your storage 在这里,您可以捕获屏幕并将其保存在存储中

give permissions for creating file in external storage 授予在外部存储中创建文件的权限

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

And this is code for activity. 这是活动的代码。

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

This is how you can capture the screen. 这是你捕捉屏幕的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM