4

I am developing an application in which I have to take screen-shot of app screen right now I used below code it is not working. I am null bitmap image

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

String screenShot =  Environment.getExternalStorageDirectory().toString()+"/bs_score_img.png";
Log.i("TAG:Score: screenShot path=", screenShot);
Bitmap bmp = BitmapFactory.decodeFile(new File(screenShot).getAbsolutePath());
hharry_tech
  • 952
  • 1
  • 8
  • 24
  • Check this [link](http://stackoverflow.com/questions/11430418/capture-screen-shot-on-android-using-java-code) – Lal Apr 10 '14 at 05:36

6 Answers6

0

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();

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

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

or try this :

View v1 = L1.getRootView();
                    v1.setDrawingCacheEnabled(true);
                    Bitmap bm = v1.getDrawingCache();
                    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
                    image = (ImageView) findViewById(R.id.screenshots);
                    image.setBackgroundDrawable(bitmapDrawable);
Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
0

I found this code from another post:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

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

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Community
  • 1
  • 1
Andynedine
  • 441
  • 5
  • 20
0

Let's say that you clicked on button

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
       Bitmap bitmap = takeScreenshot();
       saveBitmap(bitmap);
   }
});

After that you need this two methods

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   return rootView.getDrawingCache();
}

 public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0
public Bitmap screenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
            view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

just call this method with the view you want a snapshot of.. so if you want the whole screen just pass in you top most ViewGroup. if you want the System controls also just call:

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

you can try this

   private Bitmap getScreen(){    
    mDecorView = getWindow().getDecorView();
    runOnUiThread(new Runnable() {
    public void run() {
    mDecorView.invalidate();
    mDecorView.post(this);
    }
    });
    View v1 = mDecorView.getRootView();
    System.out.println("Root View : "+v1);
    v1.setDrawingCacheEnabled(true);
    return v1.getDrawingCache();
 }   

here mDecorView is View .

maddy d
  • 1,530
  • 2
  • 12
  • 23
0

Here how you can capture screen and save it in your storage

give permissions for creating file in external storage

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

And this is code for 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();
}
}

This is how you can capture the screen.

Bhaumik Thakkar
  • 580
  • 1
  • 9
  • 28