Android的 - 采取截屏Android

2023-09-05 00:53:34 作者:花落微凉梦清幽

我需要一个屏幕截图并保存屏幕截图。我需要做到这一点,而无需使用电脑或不unrooting手机的任何连接。我需要这样做每当事件被触发。例如,当一个广告被显示在游戏...或者当游戏结束,并显示在蛇等几十可否请您让我知道我该怎么做。我看到一些tutorilas,他们给了code,但,这并不似乎工作

 私人无效getScreen()
   {
    查看内容= findViewById(R.id.layoutRoot);
    点阵位图= content.getDrawingCache();
    档案文件=新的文件(/ SD卡/ test.png);
    尝试
    {
        file.createNewFile();
        FileOutputStream中的ostream =新的FileOutputStream(文件);
        bitmap.com preSS(比较pressFormat.PNG,100,ostream的);
        ostream.close();
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }
}
 

解决方案

可以提供更多的信息,当您运行code什么行不通?这岂不是捕捉到你想要什么?它会崩溃?

Android 11调整优化 加入快速截屏和A B分区切换

请确保您 R.id.layoutroot 正确的与你的根布局改变......除此之外,它似乎像它会工作...

 < com.example.android.snake.SnakeView
 机器人:ID =@ + ID /蛇
    机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            tileSize =24
            />

< RelativeLayout的
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <的TextView
     机器人:ID =@ + ID /文
        机器人:文本=@字符串/ snake_layout_text_text
        机器人:能见度=看见
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerInParent =真
        机器人:重力=center_horizo​​ntal
        机器人:文字颜色=#ff8888ff
        机器人:TEXTSIZE =24sp/>
< / RelativeLayout的>
 

编辑...

所以,举例来说,如果你使用,你只要把那里的布局,您应该修改 R.id.layout R.id。蛇,那是因为这行:机器人:ID =@ + ID /蛇

我不认为有一个简单的方法来找到/得到一个视图的根布局的ID(如果你想利用任何手机显示屏幕截图。

我刚检查了发射器和其他应用程序,似乎最喜欢的应用程序都置于与ID /内容的FrameLayout,所以你可以尝试使用 android.R.id.content ,但没有保证这会工作,每次...

I need to take a screen shot and save the screen shot. I need to do this without using any connection to PC or without unrooting the phone. I need to do this whenever a event is triggered . For example when an ad is shown in a game ... or when the game ends and shows the scores in snake etc. Can you please let me know How Can i do this. I saw some tutorilas and they gave the code but that doesnt seem to work

private void getScreen()
   {
    View content = findViewById(R.id.layoutRoot);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File("/sdcard/test.png");
    try 
    {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

解决方案

Can you give more information as to what does not work when you run that code ? Does it not capture what you want ? Does it crash ?

Make sure you change the R.id.layoutroot correctly with your root layout... Beside that it seems like it would work...

<com.example.android.snake.SnakeView
 android:id="@+id/snake"
    android:layout_width="match_parent"
            android:layout_height="match_parent"
            tileSize="24"
            />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
     android:id="@+id/text"
        android:text="@string/snake_layout_text_text"
        android:visibility="visible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:textColor="#ff8888ff"
        android:textSize="24sp"/>
</RelativeLayout>

Edit...

So for example, if you use that layout you just put there, you should change the R.id.layout into R.id.snake, that's because of this line : android:id="@+id/snake".

I don't think there is an easy way to find /get the id of the "root" layout of a view (if you wanted to take screenshot of anything the phone is showing.

I just checked the launcher and another application, seems like most app are placed into a FrameLayout with id/content, so you can try to use android.R.id.content, but there is no guaranty this will work every time...