Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take snapshot of screen not only app in Android with code

I've developed app that takes screenshot. But it only takes snapshot of app. I want to take snapshot out of app. I've researched answers but I don't find answer yet. Here is my code.

View view = getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
saveImageToAppFolder(bitmap);

saveImagetoAppFolder is function that saves image to app folder. That's not problem. Is there anyway to take snapshot of screen?

like image 579
Dima Avatar asked Dec 30 '15 04:12

Dima


2 Answers

To take screen shot of the device screen, Only if you have root call the screencap binary like:

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream  os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + Environment.getExternalStorageDirectory()+ "/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor()

And to load that file into a bitmap,Use

public static Bitmap decodeSampledBitmapFromFile(String path,
                                                     int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
like image 183
insomniac Avatar answered Nov 10 '22 18:11

insomniac


i don't know your code in saveImageToAppFolder is what but you can try this:

Note: you need set background of your app/activity to transparent (100%).

 //your code below is extractly
View view = getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

  //try my code for save image file to storage   
  File imgFile = new File(imgPath);

        FileOutputStream os = new FileOutputStream(imageFile);
        int imgQuality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, imgQuality , os);
        os.flush();
        os.close();

Code to set transparent background:

//first: create theme xml below for transparent

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

after set by this way:

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">

</activity>

note: you can red more detail from here url: How do I create a transparent Activity on Android?

like image 45
GiapLee Avatar answered Nov 10 '22 17:11

GiapLee