private void shareComicImage() { Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/*"); Bitmap mBitmap = mComicMap.get(sFavoriteIndex).getBitmap(); try { String path = MediaStore.Images.Media.insertImage( getActivity().getContentResolver(), mBitmap, "Image Description", null); share.putExtra(Intent.EXTRA_STREAM, Uri.parse(path)); } catch (Exception e) { try { File cachePath = new File(getActivity().getCacheDir(), "images"); cachePath.mkdirs(); // don't forget to make the directory FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); stream.close(); File imagePath = new File(getActivity().getCacheDir(), "images"); File newFile = new File(imagePath, "image.png"); Uri contentUri = FileProvider.getUriForFile( getActivity(), "com.gadgetmonks.carntoonage.fileprovider", newFile); share.putExtra(Intent.EXTRA_STREAM, contentUri); } catch (IOException e2) { e.printStackTrace(); } } if (PrefHelper.shareAlt()) { share.putExtra(Intent.EXTRA_TEXT, PrefHelper.getAlt(sFavorites[sFavoriteIndex])); } startActivity(Intent.createChooser(share, this.getResources().getString(R.string.share_image))); }