2

这是我的Screenshot class。该应用程序将屏幕截图保存在我的画廊中,这是我想要的,但图像完全是黑色的!如果您对这项工作有任何建议,他们将不胜感激!谢谢!

public class Screenshot extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

          // set event listener for the Save Contact Button
          Button button = 
             (Button) findViewById(R.id.button);
          button.setOnClickListener(buttonClicked);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.screenshot, menu);
        return true;
    }

       // responds to event generated when user clicks the Done Button
       OnClickListener buttonClicked = new OnClickListener() 
       {
          @Override
          public void onClick(View v) 
          {
              Bitmap bitmap = takeScreenshot();
              saveBitmap(bitmap);
           }         
       };


     public void saveBitmap(Bitmap bitmap) {
         LinearLayout mainLayout = (LinearLayout) findViewById(R.id.LinearLayout01);
         Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(), mainLayout.getHeight(), 
                 Bitmap.Config.ARGB_8888);
         MediaStore.Images.Media.insertImage(getContentResolver(), b, "image.png" , "screenshot");
    }

    public Bitmap takeScreenshot() {
         View rootView = findViewById(android.R.id.content).getRootView();
         rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
        }
}

这是我的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LinearLayout01"
>
<Button
android:id="@+id/button"
android:layout_height = "wrap_content"
android:layout_width ="wrap_content"
android:text = "take screenshot"
android:onClick = "DoIt"
/>

<ImageView
android:id="@+id/eiffelTowerImageView"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_toRightOf="@+id/colosseumImageView"
android:src="@drawable/eiffeltower" />
</LinearLayout>
4

3 回答 3

3

这段代码对我来说就像一个魅力

private static Bitmap takeScreenShot(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay()
            .getHeight();

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
            - statusBarHeight);
    view.destroyDrawingCache();
    Log.e("Screenshot", "taken successfully");
    return b;

}

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory()
            + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        Log.e("Screenshot", "saved successfully");

        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }

}

在 OnCreate() 中获取活动

Activity activity = (MainActivity) this;

然后在任何你想要的地方调用这些函数

Bitmap bitmap = takeScreenShot(activity);
saveBitmap(bitmap);
于 2015-07-24T07:07:26.720 回答
0

试试这个代码:

public class MainActivity extends Activity {

    Button btn_shoot;
    int i = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        btn_shoot = (Button)findViewById(R.id.btn_shoot);
        btn_shoot.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                    View view = findViewById(R.id.relativelayout);
                    view = view.getRootView();
                    view.setDrawingCacheEnabled(true);

                      view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

                          view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

                    Bitmap bitmap = view.getDrawingCache();

                    BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                    ImageView iv = (ImageView) findViewById(R.id.imageView1);
                    iv.setBackgroundDrawable(bitmapDrawable);

                    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                        //we check if external storage is available, otherwise display an error message to the user

                           File sdCard = Environment.getExternalStorageDirectory();
                           File directory = new File (sdCard.getAbsolutePath() + "/Tutorial_ScreenShot");
                           directory.mkdirs();

                           String filename = "screenshot" + i + ".jpg"; 
                           File yourFile = new File(directory, filename);

                           while (yourFile.exists()) {
                            i++;   
                            filename = "screenshot" + i + ".jpg"; 
                            yourFile = new File(directory, filename);
                           } 

                           if (!yourFile.exists()) {
                               if (directory.canWrite())
                               {
                                   try {
                                     FileOutputStream out = new FileOutputStream(yourFile, true);
                                     bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                                     out.flush();
                                     out.close();
                                     Toast.makeText(MainActivity.this, "File exported to /sdcard/Tutorial_ScreenShot/screenshot" + i + ".jpg", Toast.LENGTH_SHORT).show();
                                     i++;
                                    } catch (IOException e) {
                                   e.printStackTrace();
                                }

                               }
                           }

                        }
                        else
                        {
                           Toast.makeText(MainActivity.this, "SD Card not available!", Toast.LENGTH_SHORT).show();
                        }


            }
        });

    }

}
于 2014-04-07T11:46:54.843 回答
0

您的解决方案仅适用于截取您自己的应用程序的屏幕截图(因为它使用应用程序的绘图缓存),所以如果这是您想要做的,您必须确保它在屏幕上可见。您的应用程序无权查看的任何内容(例如在其后面运行的另一个应用程序)都不会显示,或者如您所见,将显示为黑色。

我建议您在此处查看其他屏幕截图问题,以了解您工作的限制条件。首先,由于您可能没有 root 权限或签名应用程序(只有在您编译自己的 ROM 时才会如此),所以您无法捕获帧缓冲区,这是 4.0+ 中的原生 Android 屏幕截图机制可以如果您按下特定的组合键(在我的 Galaxy Nexus 上是电源+音量降低),则为您服务。

于 2013-05-02T14:26:29.117 回答