Skip to content

Instantly share code, notes, and snippets.

@farooqkhan003
Created September 10, 2017 11:02
private void shareImage(Bitmap bitmap){
// save bitmap to cache directory
try {
File cachePath = new File(this.getCacheDir(), "images");
cachePath.mkdirs(); // don't forget to make the directory
FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
File imagePath = new File(this.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment