Skip to content

Instantly share code, notes, and snippets.

@zeero0
Last active September 7, 2018 06:02
Show Gist options
  • Save zeero0/8639a34e82f33a857fd3c30cbc32dfaa to your computer and use it in GitHub Desktop.
Save zeero0/8639a34e82f33a857fd3c30cbc32dfaa to your computer and use it in GitHub Desktop.
Android share intent to share text and image.
void onClickShareButton() {
String url = "URL Of the image that you want to share.";
new BitmapAsyncTask().execute(url);
}
class BitmapAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showLoader();
}
@Override
protected Bitmap doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
shareImage(bitmap);
hideLoader();
}
}
/////////////////// Try this If you dont have Image URL and want to share an image which is already shown in an ImageView.
void onClickShareButton() {
shareImage(createBitmapFromView(mImageView));
}
public Bitmap createBitmapFromView(View v) {
v.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT));
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return bitmap;
}
private void shareImage(Bitmap bitmap) {
// save bitmap to cache directory
try {
hideLoader();
File cachePath = new File(mContext.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(mContext.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", 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, mContext.getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
}
/////////////////////// For shar
void shareText() {
String shareBody = "Text you want to share.";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
}
@zeero0
Copy link
Author

zeero0 commented Sep 7, 2018

Android share intent to share text and image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment