Skip to content

Instantly share code, notes, and snippets.

@geekykant
Created November 28, 2019 08:03
Show Gist options
  • Save geekykant/1b2b62d63bc77bd1fd334a2d16b851ba to your computer and use it in GitHub Desktop.
Save geekykant/1b2b62d63bc77bd1fd334a2d16b851ba to your computer and use it in GitHub Desktop.
Intent to Whatsapp with Image & Text
int resID = mContext.getResources().getIdentifier(category.getFile_name(), "drawable", mContext.getPackageName());
// create file from drawable image
Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), resID);
File filesDir = mContext.getFilesDir();
File imageFile = new File(filesDir, category.getFile_name() + ".jpeg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bm.compress(Bitmap.CompressFormat.JPEG, 100, os); // 100% quality
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
// create new Intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, "Merry Christmas! 🎅");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, imageFile);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/jpeg");
mContext.startActivity(Intent.createChooser(intent, "send"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment