Sharing via Intent - Text and Image Sharing

We can share Image as well text using Intent, as Intent are the asynchronous messages between components of application or with third part application.


Text Sharing


void shareText(String body,String subject) {
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    sharingIntent.putExtra(Intent.EXTRA_TEXT, body);
    startActivityForResult(Intent.createChooser(sharingIntent, "Share via"), 0);
}

Image Sharing

private void shareBitmapImage (Bitmap bitmap,String imageName) {
    try {
        File file = new File(getContext().getCacheDir(), imageName + ".png");
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        file.setReadable(true, false);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("image/png");
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Comments