Android 安卓——截屏

Android 安卓——截屏,android,bitmap,screenshot,Android,Bitmap,Screenshot,我需要截屏并保存截屏。我需要在不使用任何PC连接或不打开手机的情况下执行此操作。每当触发事件时,我都需要这样做。例如,在游戏中显示广告时。。。或者当游戏结束并显示snake等的分数时,你能告诉我怎么做吗。我看了一些教程,他们给出了代码,但似乎不起作用 private void getScreen() { View content = findViewById(R.id.layoutRoot); Bitmap bitmap = content.getDrawingCache(

我需要截屏并保存截屏。我需要在不使用任何PC连接或不打开手机的情况下执行此操作。每当触发事件时,我都需要这样做。例如,在游戏中显示广告时。。。或者当游戏结束并显示snake等的分数时,你能告诉我怎么做吗。我看了一些教程,他们给出了代码,但似乎不起作用

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

您能否提供更多信息,说明在运行该代码时哪些不起作用?它没有捕捉到你想要的东西吗?它会崩溃吗

确保使用根布局正确更改
R.id.layoutroot
。。。除此之外,它似乎会起作用

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


编辑

例如,如果您使用刚才放在那里的布局,您应该将
R.id.layout
更改为
R.id.snake
,这是因为这一行:
android:id=“@+id/snake”

我不认为有一种简单的方法可以找到/获取视图“根”布局的id(如果你想拍摄手机显示的任何内容的屏幕截图)


我刚刚检查了启动程序和另一个应用程序,似乎大多数应用程序都放置在带有id/内容的框架布局中,因此您可以尝试使用
android.R.id.content
,但无法保证每次都能正常运行…

在调用getDrawingCache()之前,您必须先.

可能重复,它说它无法解析R.id.layoutroot。因此,您能告诉我如何将其映射到正确的layoutroot吗。您能发布main.xml或您要截图的视图所使用的布局文件吗?至少其中一个…我对此不太了解…我粘贴了snakelayout.xml。这是e您正在谈论的一个或是否有其他文件。因此,如果我想对任何应用程序进行屏幕截图,我需要获得根布局,这对所有应用程序都不相同…我也尝试了内容…它给出了相同的错误。@Matthieu感谢它起作用。我还添加了此语句内容。setDrawingCacheEnabled(true);还有你说的那句话。
View ve = findViewById(R.id.loyout);
        ve.setDrawingCacheEnabled(true);
        Bitmap b = ve.getDrawingCache();

        String extr = Environment.getExternalStorageDirectory().toString()
                + "/SaveCapture";
        myPath = new File(extr);

        if (!myPath.exists()) {
            boolean result = myPath.mkdir();
            Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show();
        }
        myPath = new File(extr, getString(R.string.app_name) + ".jpg");

        Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show();
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(myPath);

            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            MediaStore.Images.Media.insertImage(getContentResolver(), b,
                    "Screen", "screen");


        }

        catch (FileNotFoundException e) {

            Log.e("Error", e + "");
        }

        catch (Exception e) {

            Log.e("Error", e + "");
        }