1

I wanted to make my app be able to take proper screenshots like my own device does on touch of a button but it appears that it just takes screenshot of the view. What I want is to be able to take full screenshots with whatever it is on the screen like my device does.

Here's my java code:

package com.arvisapps.lazyscreenshot;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class MainActivity extends Activity {


RelativeLayout R1;
ImageView img;

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

private void showNotification() {
    builder.setAutoCancel(false);
    builder.setContentTitle("Take Screenshot");
    builder.setContentText("");
    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setOngoing(true);

    Notification notification = builder.build();
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    manager.notify(1, notification);
}

private void dontShowNotification(){
    NotificationManager nMgr = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    nMgr.cancel(1);
}

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    R1 = (RelativeLayout) findViewById(R.id.relatv);
    CheckBox cb1 = (CheckBox) findViewById(R.id.cb_1);

    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked == true) {
                showNotification();
            } else if (isChecked == false) {
                dontShowNotification();
            }
        }
    });
}

public void takeScreenshot(View v)
{
    View v1 = R1.getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bm = v1.getDrawingCache();
    BitmapDrawable bmDrawable = new BitmapDrawable(bm);
    img = (ImageView) findViewById(R.id.screenshots);
    img.setBackgroundDrawable(bmDrawable);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


}

Here's my XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relatv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<RelativeLayout
    android:id="@+id/cb1_grp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/cb1_txtvw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="@string/cb1_txt" />

    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/cb1_txtvw"
        android:layout_marginLeft="107dp"
        android:layout_marginStart="107dp"/>


</RelativeLayout>

<LinearLayout

    android:id="@+id/edittxt_grp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/cb1_grp"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/edittxt_txtvw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/delaytime" />

    <EditText
        android:id="@+id/edittxt_delay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"/>

</LinearLayout>

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/edittxt_grp"
    android:layout_centerHorizontal="true"
    android:text="@string/btn1_txt"
    android:onClick="takeScreenshot"/>
<ImageView
    android:layout_below="@id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/screenshots"
    android:contentDescription="@string/app_name"
    />

4127157
  • 1,438
  • 2
  • 15
  • 28
  • refer http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android –  May 29 '15 at 04:51
  • Read my question first. I want to take the screenshot of the whole screen and whatever is on the screen. What is stated in that question's answer takes the screenshot of the view of the app itself. – 4127157 May 29 '15 at 04:56

2 Answers2

1

If your phone is rooted try.

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();
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • This is still not taking the screenshot of everything on screen... just what is inside the app. I want it to capture just like phone's native screenshot does. – 4127157 May 29 '15 at 05:07
  • if device is rooted phone then we can get everything on the screen, if it is device not rooted use http://code.google.com/p/android-screenshot-library/ – Krishna V May 29 '15 at 05:13
  • My device is rooted yet I don't get everything on my screen – 4127157 May 29 '15 at 05:15
  • Getting Java IOException everywhere. – 4127157 May 29 '15 at 05:30
0

The method you're using, getDrawingCache() will only get that view's drawing cache. Unfortunately, an application cannot directly access the framebuffer (and thus the raw pixels) unless your device is rooted so you won't be able to take a full screenshot from your app.

David M
  • 841
  • 6
  • 23