-4

I want to create an app and set background with the screenshot of current screen look like , precisely saying the app should work on service and should take screen shot and set as background

what I have to do to get it?, ( take a screenshot of app foreground, any way?)

I CAN GET IT, thank @Aspicas enter image description here

Cœur
  • 34,719
  • 24
  • 185
  • 251
AndroidLTG
  • 85
  • 1
  • 11

1 Answers1

1

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

First, add proper permission to save file:

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

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

private void takeScreenshot() {
    Date now = new Date();
    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 file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

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

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 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:

private void openScreenshot(File imageFile) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(imageFile);
    intent.setDataAndType(uri, "image/*");
    startActivity(intent);
}

Found Here

Edit

To make a Transparent layout

Add the following style In your res/values/styles.xml file (if you don’t have one, create it.) Here’s a complete file:

<?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>

(the value @color/transparent is the color value #00000000 which I put in res/values/color.xml file. You can also use @android:color/transparent in later Android versions)

Then apply the style to your activity on your AndroidManifest.xml, for example:

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

Edit 2

You can put a transparent Background too on one LinearLayout, Relative, table.... down alpha on your ParseColor:

#30000000

Thanks to @FrankN.Stein

The first 30" is called Alpha and indicates the opacity, but not as a percentage. It's a hex number ranging from 00 to ff (ff is the default if omitted)

Community
  • 1
  • 1
Aspicas
  • 4,483
  • 4
  • 29
  • 53