0

I have an application which takes screenshots and saves them which works correctly. However, now when I go to view the screenshot it either has a black background or it just shows a black screen. How can I return the actual screenshot of my application?

MainActivity

public class ScreenshotActivity extends Activity {

    Button btn_screenshoot;
    int i = 0;
    ImageView imgv_showscreenshot;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screenshotlayout);

        btn_screenshoot = (Button) findViewById(R.id.btn_screenshoot);
        btn_screenshoot.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
                View view = findViewById(R.id.relativelayout);
                view.setDrawingCacheEnabled(true);
                Bitmap bitmap = view.getDrawingCache();
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                imgv_showscreenshot = (ImageView) findViewById(R.id.imgv_showscreenshot);
                // set screenshot bitmapdrawable to imageview
                imgv_showscreenshot.setBackgroundDrawable(bitmapDrawable);

                if (Environment.MEDIA_MOUNTED.equals(Environment
                        .getExternalStorageState())) {
                    // we check if external storage is available, otherwise
                    // display an error message to the user using Toast Message
                    File sdCard = Environment.getExternalStorageDirectory();
                    File directory = new File(sdCard.getAbsolutePath()
                            + "/DCIM/Images");
                    directory.mkdirs();

                    String filename = "screenshot" + i + ".jpg";
                    File yourFile = new File(directory, filename);

                    while (yourFile.exists()) {
                        i++;
                        filename = "screenshot" + i + ".jpg";
                        yourFile = new File(directory, filename);
                    }

                    if (!yourFile.exists()) {
                        if (directory.canWrite()) {
                            try {
                                FileOutputStream out = new FileOutputStream(
                                        yourFile, true);
                                bitmap.compress(Bitmap.CompressFormat.PNG, 90,
                                        out);
                                out.flush();
                                out.close();
                                Toast.makeText(
                                        ScreenshotActivity.this,
                                        "File exported to: " + yourFile.getAbsolutePath()+ i + ".jpg",
                                        Toast.LENGTH_SHORT).show();
                                i++;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }

                } else {
                    Toast.makeText(ScreenshotActivity.this,
                            "Sorry SD Card not available in your Device!",
                            Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btn_screenshoot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Capture" />

    <ImageView
        android:id="@+id/imgv_showscreenshot"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_below="@+id/btn_screenshoot"
        android:layout_marginTop="10dp" />

</RelativeLayout>

Android Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidsurya.screenshot"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidsurya.screenshot.ScreenshotActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <intent-filter>
  <action android:name="android.intent.action.MEDIA_MOUNTED" />
  <data android:scheme="file" /> 
</intent-filter>
    </application>

</manifest>
Mr Capital
  • 33
  • 1
  • 9

1 Answers1

0

Check your phone is rooted.If your phone is rooted try this

Process sh = Runtime.getRuntime().exec("su", null,null);

                OutputStream  os = sh.getOutputStream();
                os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                os.flush();

                os.close();
                sh.waitFor();

then read img.png as bitmap and convert it jpg as follows

Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+File.separator +"img.png");

//my code for saving
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.JPEG, 15, bytes);

//you can create a new file name "test.jpg" in sdcard folder.

File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput

fo.close();

you have no access to the screen if your application is in background unless you are rooted, the code above can take the screenshot most effectively of any screen even if you are in background.

UPDATE

Google has a library with which you can take screenshot without rooting, I tried that, But iam sure that it will eat out the memory as soon as possible.

Try http://code.google.com/p/android-screenshot-library/

taken from Android - How to take screenshot programatically

Community
  • 1
  • 1
likith sai
  • 527
  • 1
  • 6
  • 21