Search code examples
androidscreenshotandroid-screen

Capture Screen Programmatically not working


I have following method to Capture Screen on Action Item Click. Its working on Android <2.3 but not on 4+. What is wrong with this way of screen capture.

private void captureScreen() {
    View v = mapView.getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap capturedBitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);

    if(capturedBitmap != null) {
        Intent intent = new Intent(this, ScreenCapturedAlertActivity.class);
        intent.putExtra("capturedImage", capturedBitmap);
        intent.putExtra("name", location.getName());
        startActivity(intent);
    } else {
        Toast.makeText(this, "Screen Capture Failed", Toast.LENGTH_SHORT).show();
    }
}

The ScreenCaputureAlertActivity.java >>>

public class ScreenCapturedAlertActivity extends SherlockActivity {

private ImageView capturedImage;
private Bitmap capturedBitmap;
private String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_screencaptured_alert);

    capturedBitmap = (Bitmap) getIntent().getParcelableExtra("capturedImage");
    name = getIntent().getStringExtra("name");

    capturedImage = (ImageView) findViewById(R.id.ivCapturedImage);
    capturedImage.setImageBitmap(capturedBitmap);
}

private void saveAndShare(boolean share) {
    String root = Environment.getExternalStorageDirectory().toString();
    File dir = new File(root + "/capture/");
    if(!dir.exists())
        dir.mkdirs();

    FileOutputStream outStream = null;
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    File file = new File(dir, "Capture "+n+".jpg");
    if(file.exists()) {
        file.delete();
    }
    try {
        outStream = new FileOutputStream(file);

        capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

        outStream.flush();
        outStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
        return;
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
        return;
    }

    if(share) {
        Uri screenshotUri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        intent.putExtra(Intent.EXTRA_SUBJECT, "Location of " + name);
        intent.putExtra(Intent.EXTRA_TITLE, getText(R.string.screen_share_message));
        intent.putExtra(Intent.EXTRA_TEXT, getText(R.string.screen_share_message));
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent, "Share with"));
        finish();
    } else {
        Toast.makeText(this, "Save Success", Toast.LENGTH_SHORT).show();

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        finish();
    }
}

public void saveCapture(View view) {
    saveAndShare(false);
}

public void shareCapture(View view) {
    saveAndShare(true);
}

}


Solution

  • Thanks to @KumarBibek guidance.

    The error I was getting was

    !!! FAILED BINDER TRANSACTION !!!
    

    So as from the selected answer from the link

    Send Bitmap as Byte Array

    I did like this in first activity:

    private void captureScreen() {
        View v = mapView.getRootView();
        v.setDrawingCacheEnabled(true);
        Bitmap capturedBitmap = Bitmap.createBitmap(v.getDrawingCache());
        v.setDrawingCacheEnabled(false);
    
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        capturedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
    
        if(capturedBitmap != null) {
            Intent intent = new Intent(this, ScreenCapturedAlertActivity.class);
            intent.putExtra("capture", byteArray);
            intent.putExtra("name", location.getName());
            startActivity(intent);
        } else {
            Toast.makeText(this, "Screen Capture Failed", Toast.LENGTH_SHORT).show();
        }
    
    }
    

    And in ScreenCapturedAlertActivity :

    byte[] byteArray = getIntent().getByteArrayExtra("capture");
            capturedBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    

    It is working WELL now. Thanks again to @KumarBibek