0

Hey there so I want to create a App in android which have a overlayed Button and take a screenshot (if overlay Button is clicked) of every app on the phone (anything except these with FLAG_SECURE). I know that it's possible because this app actually do it: https://play.google.com/store/apps/details?id=com.tools.screenshot

So does anyone knows a code example which allows me to do this screenshot? I already got the overlay Button so only need the screenshot code. Thanks

Luca486
  • 53
  • 1
  • 10
  • On Android 5.0+, use the media projection APIs. – CommonsWare Jan 26 '17 at 16:38
  • 2
    Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Prerak Sola Jan 26 '17 at 16:40
  • @CommonsWare do you know a reference or a sample code? I found some samplecodes but I can't really implement it to my code or in an Service :( – Luca486 Jan 26 '17 at 17:15
  • 1
    https://github.com/commonsguy/cw-omnibus/tree/master/MediaProjection/andshooter – CommonsWare Jan 26 '17 at 17:17
  • @CommonsWare Can you give me a tip how to implement this in my code? I added the Activitys and the Service from your example code to my Project. And my Overlay Button start OnCLick the "MainActivity" from your Example Code. If I do it like that the App crashes and there is no Exeception in the Android Monitor. How can I implement it better? – Luca486 Jan 26 '17 at 17:57
  • "If I do it like that the App crashes and there is no Exeception in the Android Monitor" -- if you are crashing, then there will be a stack trace in the Monitor. Otherwise, by definition, you are not crashing. Note that you may need to remove some of the Monitor filters to see the stack trace. In terms of using the sample code, that is well beyond the scope of a Stack Overflow question and answer. I cover it in [my book](https://commonsware.com/Android); here is [a preview edition of the chapter](https://commonsware.com/Android/previews/screenshots-and-screen-recordings). – CommonsWare Jan 26 '17 at 18:01

1 Answers1

0

Too broad as answer, but had this from old blog:

taking_screenshot_in_android.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="16dp"
android:gravity="center"
android:orientation="vertical">


<Button
    android:id="@+id/capture_screen_shot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="screenShot"
    android:text="Take ScreenShot" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:text="" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="200dp"
    android:layout_height="250dp"
    android:background="#ccc"
    android:padding="5dp" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:autoLink="web"
    android:gravity="center|bottom"
    android:text="ViralAndroid.com"
    android:textSize="26sp"
    android:textStyle="bold" />
</LinearLayout>

TakingScreenShotAndroid.java

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class TakingScreenShotAndroid extends AppCompatActivity {

TextView textView;
ImageView imageView;
Bitmap mbitmap;
Button captureScreenShot;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.taking_screenshot_in_android);


    textView = (TextView) findViewById(R.id.textView);
    textView.setText("Your ScreenShot Image:");

    captureScreenShot = (Button) findViewById(R.id.capture_screen_shot);
    imageView = (ImageView) findViewById(R.id.imageView);

}

public void screenShot(View view) {
    mbitmap = getBitmapOFRootView(captureScreenShot);
    imageView.setImageBitmap(mbitmap);
    createImage(mbitmap);
}

public Bitmap getBitmapOFRootView(View v) {
    View rootview = v.getRootView();
    rootview.setDrawingCacheEnabled(true);
    Bitmap bitmap1 = rootview.getDrawingCache();
    return bitmap1;
}

public void createImage(Bitmap bmp) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
    File file = new File(Environment.getExternalStorageDirectory() +
            "/capturedscreenandroid.jpg");
    try {
        file.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(file);
        outputStream.write(bytes.toByteArray());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }

}

strings.xml

<string name="app_name">Taking Screenshot in Android Programmatically</string>

Output:

enter image description here

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • This Code only take a screenshot of my own Application right? – Luca486 Jan 26 '17 at 17:13
  • @Luca486 yes, it does the same. But if you want you can put this code in your widget or do manipulation. It's a general screenshot code. – W4R10CK Jan 26 '17 at 17:15