how to Capture screen in android and covert it to image [duplicate]

∥☆過路亽.° 提交于 2019-12-04 14:53:46

问题


Possible Duplicate:
Screenshot Android

Any one please Help me with this..

how to capture screen content in android programmatically/.//

Thanks in Advance..


回答1:


Try this code, don't forget to add this permission in your Manifest.xml

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

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

ScreenCaptureActivity.java

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

    }

}



回答2:


As you need to use same code described in question priviously asked

// image naming and path  to include sd card  appending name you choose for file
 String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

 // create bitmap screen capture
 Bitmap bitmap;
 View v1 = mCurrentUrlMask.getRootView();
 v1.setDrawingCacheEnabled(true);
 bitmap = Bitmap.createBitmap(v1.getDrawingCache());
 v1.setDrawingCacheEnabled(false);

 OutputStream fout = null;
 imageFile = new File(mPath);

 try {
     fout = new FileOutputStream(imageFile);
     bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
     fout.flush();
     fout.close();

 } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
     e.printStackTrace();
 } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
 }

as described in this question

How to programmatically take a screenshot in Android?



来源:https://stackoverflow.com/questions/11360664/how-to-capture-screen-in-android-and-covert-it-to-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!