Skip to content Skip to sidebar Skip to footer

How To Take Screen-shot Of App Screen In Android?

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.getRunti

Solution 1:

try this :

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

                    OutputStreamos= 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 

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

//my code for savingByteArrayOutputStreambytes=newByteArrayOutputStream();
    screen.compress(Bitmap.CompressFormat.JPEG, 15, bytes);

//you can create a new file name "test.jpg" in sdcard folder.Filef=newFile(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
            f.createNewFile();
//write the bytes in fileFileOutputStreamfo=newFileOutputStream(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 :

Viewv1= L1.getRootView();
                    v1.setDrawingCacheEnabled(true);
                    Bitmapbm= v1.getDrawingCache();
                    BitmapDrawablebitmapDrawable=newBitmapDrawable(bm);
                    image = (ImageView) findViewById(R.id.screenshots);
                    image.setBackgroundDrawable(bitmapDrawable);

Solution 2:

I found this code from another post:

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

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

OutputStreamfout=null;
imageFile = newFile(mPath);

try {
    fout = newFileOutputStream(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();
}

Solution 3:

Let's say that you clicked on button

findViewById(R.id.button1).setOnClickListener(newOnClickListener() {
   @OverridepublicvoidonClick(View v) {
       Bitmapbitmap= takeScreenshot();
       saveBitmap(bitmap);
   }
});

After that you need this two methods

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

 publicvoidsaveBitmap(Bitmap bitmap) {
    FileimagePath=newFile(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = newFileOutputStream(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);
    }
}

Solution 4:

public Bitmap screenShot(View view) {
    Bitmapbitmap= Bitmap.createBitmap(view.getWidth(),
            view.getHeight(), Config.ARGB_8888);
    Canvascanvas=newCanvas(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:

Solution 5:

you can try this

private Bitmap getScreen(){    
    mDecorView = getWindow().getDecorView();
    runOnUiThread(new Runnable() {
    publicvoidrun() {
    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 .

Post a Comment for "How To Take Screen-shot Of App Screen In Android?"