Android4.0以上版本屏幕截图总结

Home / Android MrLee 2016-3-29 4891

总会有些奇葩的需求,截图我原来也研究过。但是很多方法是无效的。原因也有很多,一部分需要ROOT,一部分针对Android版本才有效。今天在这里总结下,毕竟有些还是可行的。那我们针对不同的系统调用不同的截图API就可以了。
截取自身Activity的界面是最简单的,看源码:
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
   
import android.app.Activity;  
import android.graphics.Bitmap;  
import android.graphics.Rect;  
import android.view.View;  
   
public class ScreenShot {  
   
    private static Bitmap takeScreenShot(Activity activity) {  
        // View是你需要截图的View  
        View view = activity.getWindow().getDecorView();  
        view.setDrawingCacheEnabled(true);  
        view.buildDrawingCache();  
        Bitmap b1 = view.getDrawingCache();  
   
        // 获取状态栏高度  
        Rect frame = new Rect();  
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
        int statusBarHeight = frame.top;  
   
        // 获取屏幕长和高  
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();  
        int height = activity.getWindowManager().getDefaultDisplay()  
                .getHeight();  
        // 去掉标题栏  
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  
                - statusBarHeight);  
        view.destroyDrawingCache();  
        return b;  
    }  
   
    private static void savePic(Bitmap b, File filePath) {  
        FileOutputStream fos = null;  
        try {  
            fos = new FileOutputStream(filePath);  
            if (null != fos) {  
                b.compress(Bitmap.CompressFormat.PNG, 100, fos);  
                fos.flush();  
                fos.close();  
            }  
        } catch (FileNotFoundException e) {  
            // e.printStackTrace();  
        } catch (IOException e) {  
            // e.printStackTrace();  
        }  
    }  
   
    public static void shoot(Activity a, File filePath) {  
        if (filePath == null) {  
            return;  
        }  
        if (!filePath.getParentFile().exists()) {  
            filePath.getParentFile().mkdirs();  
        }  
        ScreenShot.savePic(ScreenShot.takeScreenShot(a), filePath);  
    }  

 
1、android 4.0以下版本后台截屏
通过JNI方式截取屏幕,缺点是需要源码环境,详情请参考:
http://blog.csdn.net/zmyde2010/article/details/6925498
http://www.cnblogs.com/dwayne/archive/2012/01/03/2311404.html
2、android 4.0~4.2  版本后台截屏
这个可以通过反射android.view.Surface类的 screenshot方法完成。
sc = Class.forName("android.view.Surface");
method=sc.getMethod("screenshot", new Class[] {int.class, int.class});  
Object o = method.invoke(sc, new Object[]{(int) dims[0],(int) dims[1]});  
mScreenBitmap =(Bitmap)o;

 
该方法优点无需ROOT,缺点是只支持4.0~4.2,4.3版本就不适用了。
或者通过 bufferframe读取fb0这种方法,但是估计需要ROOT权限
3、android 4.3版本后台截屏
4.3版本其实也存在screenshot方法,只不过他所属的整个类android.view.SurfaceControl都被hide了,所以通过一般的反射是无法调用的。
就是在PC端执行adb shell  /system/bin/screencap -p /sdcard/screenshot.png命令是可以正确截取屏幕的,但是换到android端执行该命令就会出错,截出来的图片大小为0.个人推测应该是android端执行shell命令出了什么问题。
在尝试了很多办法之后,终于在网上找到了解决办法。
原因就在于我们之前执行命令之前没有获取ROOT权限,就算是当前手机已经有了ROOT权限,我们也必须要在程序里再次获取ROOT权限,
也就是先执行下 Runtime.getRuntime().exec(“su”),然后在执行我们需要的命令。
然后我们就可以看到SD卡里面生成的正常的图片了。
这方面网上已经有了相应的工具包,我们开发起来可以事半功倍。
https://github.com/Trinea/android-common
而我们要做的只是把它里面的ShellUtils引用进来,然后调用execCommand(String command, boolean isRoot) 就可以了

本文链接:https://www.it72.com/8679.htm

推荐阅读
最新回复 (0)
返回