安卓:无根屏幕捕捉 [英] Android: Screen capturing without root

查看:237
本文介绍了安卓:无根屏幕捕捉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过低于code,但我运行它给出了Android手机和模拟器它给出了一个文件,打开这个文件,我得到消息的 preVIEW不可

I tried below code but as I run it gives a black screen image on android mobile and on emulator it gives a file, by opening this file I get message as preview not available.

View v = activity.getWindow().getDecorView();

    v.setDrawingCacheEnabled(true);
    v.destroyDrawingCache();

    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay().getHeight();
    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, width, height); 

    v.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false); // clear drawing cache

    FileOutputStream fos = null;
    File fTree = new File(sdCardRoot,"/fi.png");
    fTree.createNewFile();

    try {

        fos = new FileOutputStream(fTree,true);
        if (null != fos) {
            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.flush();
            fos.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

请别人帮忙。谢谢你。

推荐答案

那好吧。您可能需要重写下面的方法。现在的问题是,因为你的观点是没有创建,甚至在此之前,你试图让这实际上是不存在的,因此你没有得到任何图像的背景。

Okay then. You might have to override the below method. The problem is because your view is not created yet and even before that you are trying to get the background which is actually not existing and hence you dont get any image.

试试这个,

 @Override 
public void onWindowFocusChanged(boolean hasFocus) 
{ 
   // add the entire code for getting the background within this and it will work 
} 

该方法被调用,一旦你的看法绘制,因此你会得到所需要的输出通过重写此方法。

This method gets called, once your view is drawn and hence you will get the required output by overriding this method.

这篇关于安卓:无根屏幕捕捉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆