使用原生android API拍摄截图

时间:2012-12-26 09:23:30

标签: android api screenshot native

  

可能重复:
  how to take snapshot of screen programmatically?

作为构建我的新应用的一部分,我想允许用户使用我的应用中的按钮截取设备的屏幕截图。 但是,我需要在没有使用本机API的设备生根的情况下完成此操作。

我试图寻找引用和android开发者网站,但找不到本机API功能来执行上述操作。

请您确认确实可以执行上述操作(以编程方式截取屏幕截图)? 你能否请我到某个地方看看API?

1 个答案:

答案 0 :(得分:0)

试试这段代码:

public class ScreenShotActivity extends Activity {

    Bitmap bmScreen;
    RelativeLayout mLayout;
    Dialog screenDialog;
    static final int ID_SCREENDIALOG = 1;

    ImageView bmImage;
    Button btnScreenDialog_OK;
    // TextView TextOut;

    View screen;
    EditText EditTextIn;

    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        screen = (View) findViewById(R.id.screen);
        Button btnCaptureScreen = (Button) findViewById(R.id.capturescreen);

        btnCaptureScreen.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                screen.setDrawingCacheEnabled(true);
                bmScreen = screen.getDrawingCache();
                saveImage(bmScreen);
                // showDialog(ID_SCREENDIALOG);
            }
        });
    }

    protected void saveImage(Bitmap bmScreen2) {
        // TODO Auto-generated method stub

        // String fname = "Upload.png";
        File saved_image_file = new File(
                Environment.getExternalStorageDirectory()
                        + "/captured_Bitmap.png");
        if (saved_image_file.exists())
            saved_image_file.delete();
        try {
            FileOutputStream out = new FileOutputStream(saved_image_file);
            bmScreen2.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

同时添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

用于此的XML将是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/capturescreen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Capture Screen" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/roundcorner"
        android:scaleType="fitXY"
        android:src="@drawable/android_awesome" />

</RelativeLayout>