Skip to content Skip to sidebar Skip to footer

In Android, Is It Possible To Capture Screenshots From Service

I'm developing an Android application to capture screenshots. What I'm trying to do is make a service with a button on the top of the screen and taking screenshot by tapping it. I

Solution 1:

Here is the code that allowed my screenshot to be stored on sd card and used later for whatever your needs are:

First, add proper permission to save file:

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

And this is the code (running in an Activity):

privatevoidtakeScreenshot() {
    Datenow=newDate();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {
        // image naming and path  to include sd card  appending name you choose for fileStringmPath= Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

        // create bitmap screen captureViewv1= getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmapbitmap= Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        FileimageFile=newFile(mPath);

        FileOutputStreamoutputStream=newFileOutputStream(imageFile);
        intquality=100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        openScreenshot(imageFile);
    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
}

And this is how you can open the recently generated image:

privatevoidopenScreenshot(File imageFile) {
    Intentintent=newIntent();
    intent.setAction(Intent.ACTION_VIEW);
    Uriuri= Uri.fromFile(imageFile);
    intent.setDataAndType(uri, "image/*");
    startActivity(intent);
}

to exclude the button, you can make button hide and then take screen show after some time interval.

Post a Comment for "In Android, Is It Possible To Capture Screenshots From Service"