Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Not taking screenshot of current screen

Tags:

android

Code work fine for first screenshot and keep taking same screenshot regardless of moving to another view.

How to get current screenshot?

public void saveBitmap(Bitmap bitmap) {

    File imagePath = new File(Environment.getExternalStorageDirectory() + "/" + new SimpleDateFormat("yyyyMMddhhmmss'.jpg'").format(new Date()) );
    FileOutputStream fos =null;
    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);
    }
}

click info:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.iSave:
          Bitmap bitmap = null;
          bitmap = takeScreenshot();
          saveBitmap(bitmap);
        break;
    } 
}

here:

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   return rootView.getDrawingCache();
}
like image 837
Fou Avatar asked May 04 '15 13:05

Fou


2 Answers

Call rootView.setDrawingCacheEnabled(false); after taking the screen-shot. Turning it off and then on again forces it to update correctly.

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   Bitmap bitmap = rootView.getDrawingCache();
   rootView.setDrawingCacheEnabled(false);
   return bitmap;
}
like image 147
samgak Avatar answered Oct 24 '22 16:10

samgak


I have ever tried to capture the current Activity and then share the screenshot. This below is how I did, take a look at them if you are still interested, and I think you would agree.

First, the get the root view of current Activity:

View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

or

View rootView = findViewById(android.R.id.content);

or

View rootView = findViewById(android.R.id.content).getRootView();

Second, get Bitmap from the root view:

public static Bitmap getScreenShot(View view) {
    View screenView = view.getRootView();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
    screenView.setDrawingCacheEnabled(false);
    return bitmap;
}

Third, store the Bitmap into the SDCard:

private final static String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
public static void store(Bitmap bm, String fileName){
    File dir = new File(dir);
    if(!dir.exists())
        dir.mkdirs();
    File file = new File(dir, fileName);
    try {
        FileOutputStream fOut = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

At last, share the screenshot file:

private void shareImage(String file){
    Uri uri = Uri.fromFile(file);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(intent, "Share Screenshot"));
}
like image 41
SilentKnight Avatar answered Oct 24 '22 16:10

SilentKnight