Sri Sri
Sri Sri

Reputation: 3109

Screen Capture in android

I want to develop an application that will take screenshot of android screen..does any one know how to do it..? which is similar to koushik duttas screen shot..But with out using root..and does any one had koushik dutta screenshot application which is working..? is not working for me..please let me know..thanks in advance.

Upvotes: 11

Views: 46707

Answers (4)

zpr
zpr

Reputation: 2940

The method view.getDrawingCache() will first attempt to retrieve an image it has previously cached. This can cause issues if you want to guarantee your screenshot is up-to-date. For instance, if your user clicks your screenshot button, then changes the UI, then clicks it again, the second screenshot will be identical to the first unless you wipe the cache. I find the following method more convenient:

public Bitmap takeScreenshot() {
  View rootView = findViewById(android.R.id.content).getRootView();
  Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  rootView.draw(canvas);
  return bitmap;
}

Upvotes: 3

Kristijan Drača
Kristijan Drača

Reputation: 614

Let's say that you clicked on a button:

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

After that you need these 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.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

Upvotes: 19

Nicolas Raoul
Nicolas Raoul

Reputation: 60203

I think it is impossible without root or the SDK, sorry.

I would love to be proven wrong.

Not an app, but if you have a USB cable, you can install the Android SDK on a PC and take screenshots from the PC with androidscreencast, without having to root your phone.

Upvotes: 0

user741396
user741396

Reputation: 53

You can try something like this

private RelativeLayout view;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    view = (RelativeLayout)findViewById(R.id.relativeView);

    View v1 = view.getRootView();

    v1.setDrawingCacheEnabled(true);
    Bitmap bm = v1.getDrawingCache();
}

Upvotes: 4

Related Questions