【问题标题】:Android -- taking a screen shotAndroid——截屏
【发布时间】:2011-06-15 05:09:27
【问题描述】:

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

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

【问题讨论】:

标签: android bitmap screenshot


【解决方案1】:

在调用 getDrawingCache() 之前,您必须先enable the cache

【讨论】:

    【解决方案2】:
    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 + "");
            }
    

    【讨论】:

      【解决方案3】:

      您能否提供更多关于运行该代码时哪些内容不起作用的信息?它没有捕捉到你想要的吗?它会崩溃吗?

      确保使用根布局正确更改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/content 的 FrameLayout 中,所以你可以尝试使用android.R.id.content,但不能保证每次都能正常工作...

      【讨论】:

      • 它说它无法解析 R.id.layoutroot 。那么你能告诉我如何将它映射到正确的layoutroot。
      • 您能否发布您的 main.xml 或您用于要截屏的视图的布局文件?至少其中一个...
      • 我对此了解不多....我粘贴了snakelayout.xml。这是你说的那个还是有另一个文件。
      • 因此,如果我想截取任何应用程序的屏幕截图,我需要获取所有应用程序都不相同的根布局......我也尝试了内容......它给出了同样的错误。
      • @Matthieu 谢谢你成功了。我还添加了这个语句 content.setDrawingCacheEnabled(true);还有你告诉的 cnage。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2013-05-05
      • 2015-03-05
      相关资源
      最近更新 更多