2011-06-15 187 views
1

我需要拍摄屏幕截图并保存截屏。我需要这样做,而不使用任何连接到PC或没有根除手机。每当事件被触发时,我都需要这样做。例如,当广告在游戏中显示时......或者当游戏结束并显示蛇的得分等时。您可以让我知道我如何做到这一点。我看到一些tutorilas,他们给的代码,但似乎没有工作Android - 拍摄屏幕截图

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

po可以复制http://stackoverflow.com/questions/5929403/take-screenshot-of-android-screen-and-save-to-sd-card和http://stackoverflow.com/questions/5939987/android-take-通过代码截图 – DynamicMind 2011-06-15 07:18:37

回答

0

你能给为当您运行的代码是什么不起作用的更多信息?它没有捕获你想要的东西吗?它会崩溃吗?

确保你与你的根布局正确改变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(如果你想采取任何东西截图手机显示。

我只是检查发射器和另一个应用程序,似乎像大多数应用程序被放置到一个FrameLayout与ID /内容,所以你可以尝试使用android.R.id.content,但没有保证,这将工作每次...

+0

它说它无法解析R.id.layoutroot。所以你可以让我知道我怎样才能将它映射到正确的layoutroot。 – Vaali 2011-06-15 13:55:56

+0

你可以发布你的main.xml或布局文件,用于你想要拍摄截图的视图吗?其中至少有一个...... – Matthieu 2011-06-15 14:11:32

+0

我对这个知之甚少......我粘贴了snakelayout.xml。这是你正在谈论的还是有另一个文件。 – Vaali 2011-06-15 14:28:32

1

你必须enable the cache第一,然后调用getDrawingCache()。

+0

谢谢。它解决了我的问题的一部分。 – Vaali 2011-06-15 21:59:10

1
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 + ""); 
     }