How to Share Image+Text using Share Intent in android
Try out the below code:
String fileName = "image-3116.jpg";//Name of an image
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test Mail");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Launcher");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Deal"));
Share text and image at the same time in Android
I finally managed to do it in a simpler way! I created a base64
from the image and added it as a string in the strings.xml
file (make sure to remove data:image/png;base64,
from the start of the string).
The code I used is the following:
byte[] decodedString = Base64.decode(getString(R.string.share_image), Base64.DEFAULT); //The string is named share_image.
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Uri imageToShare = Uri.parse(MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), decodedByte, "Share image", null)); //MainActivity.this is the context in my app.
String textToShare = "Sample text"; //Text to be shared
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_TEXT, textToShare);
share.putExtra(Intent.EXTRA_STREAM, imageToShare);
startActivity(Intent.createChooser(share, "Share with"));
I hope this answer will solve many peoples issues!
Thanks a lot for @notyou & @CommonsWare
How can i share text and image in Android
Below code is useful to share both text and images with other apps.
String imageToShare = "http://s1.dmcdn.net/hxdt6/x720-qef.jpg"; //Image You wants to share
String title = "Title to share"; //Title you wants to share
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
shareIntent.setType("*/*");
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_TEXT, imageToShare);
startActivity(Intent.createChooser(shareIntent, "Select App to Share Text and Image"));
How can i send image with text with share
YOu have to save the bitmap from url into the disk first then use the file to be passed on intent.
File file = writebitmaptofilefirst("the_new_image","http://www.theimagesource.com/blahblahblah.jpg");
Uri uri = Uri.fromFile(file);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
Add this method
public static File writebitmaptofilefirst(String filename, String source) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extStorageDirectory + "/temp_images");
if (!mFolder.exists()) {
mFolder.mkdir();
}
OutputStream outStream = null;
File file = new File(mFolder.getAbsolutePath(), filename + ".png");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".png");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
URL url = new URL(source);
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file;
}
How do I share image and text together in WhatsApp
After doing a lot of trial and error, I found that the way we pass text via Intent.EXTRA_TEXT matters for WhatsApp!!
I just changed
intent.putExtra(Intent.EXTRA_TEXT, etShareMessage.text)
to
intent.putExtra(Intent.EXTRA_TEXT, etShareMessage.text.toString())
and it worked.
Related Topics
Android Studio Build.Gradle Warning Message
How to Send Data Message Using Firebase Console
Sqlite Exception No Such Column When Trying to Select
Android: Volley Http Request Custom Header
How to Draw Border on Just One Side of a Linear Layout
Get Launchable Activity Name of Package from Adb
Android-L Cardview Visual Touch Feedback
Adding Expandablelistview to Navigationview
Android: Application-Wide Font-Size Preference
Using the Limit Statement in a SQLite Query
How to Open My Existing Eclipse Projects in Android Studio
Find Current Location Latitude and Longitude in Android
How Does One Use Basic Authentication with Volley on Android
How to Get Contacts from Native Phonebook in Android
Best Ocr (Optical Character Recognition) Example in Android
What Is the Correct Way to Specify Dimensions in Dip from Java Code
How to Use the Android Keystore to Securely Store Arbitrary Strings