android Share Intent not works for text and image both - android

Hi there is the code i used to share text and image :
Toast.makeText(App.getContext(), "Share", Toast.LENGTH_LONG).show();
Intent sharingIntent = null;
if(!image_resource.isEmpty()){
sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
String imagePath = "SD-Card Path";
ExceptionHelpers.dLog("SHARE_LOG", "Share image path : " + imagePath);
ExceptionHelpers.dLog("SHARE_LOG", "Share image exist : " + (new File(imagePath).exists())); // It return 'true' on LogCat
if(new File(imagePath).exists()) {
ExceptionHelpers.dLog("SHARE_LOG", "Share image and text");
Uri imageUri = Uri.fromFile(new File(imagePath));
sharingIntent.setType("*/*"); // also 'image/*' tested and not works
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
}else{
ExceptionHelpers.dLog("SHARE_LOG", "Share text");
sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
}
}else {
ExceptionHelpers.dLog("SHARE_LOG", "Share text");
sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
}
String shareBody = ""+App.getString(R.string.this_text_shared_from)+" : "+App.getString(R.string.app_name)+" "+App.getString(R.string.share_text_2)+" \n "+
App.getString(R.string.share_about);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "" + App.getString(R.string.app_name));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "" + App.getString(R.string.share_via)));
App is my custom class and App.getString is :
public static String getString(int resId){
return App.context.getString(resId);
}
it don't works well on Telegram, Gmail and same Apps And only share text with out Image

i found the answer for it, it work for me and i sent text and image both in one message to Telegram app.here is my code.
Intent intent = new Intent(Intent.ACTION_SEND);
packageName="org.telegram.messenger";
intent.setPackage(packageName);
//this is my art....
intent.setType("image/text/plain");
path=getBitMapPath();//this is path of image
uri = Uri.parse(path);
putExtra(Intent.EXTRA_STREAM, uri);
putExtra(Intent.EXTRA_TEXT, "hello telegram. i do it!");
ss = Intent.createChooser(intent, "hamid");
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ss);

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
else
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.setType("image/*");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getActivity().startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));

Related

Sharing an image with some text under it, both in the same message

I am looking to add a text message under an image (not printed on the image) so when I click a share button, the image, and text message to be shared as one message that looks like this:
My code to share an image looks like this:
public void shareImage(View view) {
Intent shareIntent = new Intent();
Uri photoURI = FileProvider.getUriForFile(this, "com.abcd.myapp", theImage);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Image with ..."));
}
How can I add the text Image created by: abcd.com under the image, (as a text not printed on the image) in the same text message or email etc.
Try this
Uri imgUri = Uri.fromFile(new File(DIRECTORY + "/" + fileName));
//Add this code if you get SecurityException error
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Text you want to attach with image.");
startActivity(Intent.createChooser(shareIntent, "Share Image"));
The following snippet of code should work. We add the image to MediaStore.
public void shareImage(View view) {
Intent shareIntent = new Intent();
shareIntent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
+ Constant.SHARE_URL);
Uri photoURI = FileProvider.getUriForFile(this, "com.abcd.myapp", theImage);
Bitmap bm = BitmapFactory.decodeFile(photoURI);
String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Image with ..."));
}
A simple case of sharing a bitmap to Twitter/Whatsapp/Facebook Messenger was resolved thanks to #Aniruddh Chandratre solution. MediaStore.Images.Media.insertImage returns a String in content://media format i.e. content://media/external/images/media/610
val savedImageURI = MediaStore.Images.Media.insertImage(
activity.contentResolver, bitmap, "title", "decription")
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(savedImageURI))
shareIntent.type = "image/*"
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
activity.startActivity(Intent.createChooser(shareIntent, "Share Image"))

Sharing Image using intent in watsApp is not working

I am sharing two URI image resource which from mipmap and ACTION_GET_CONTENT used URI.
public void shareUsingIntent() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getUri());
startActivity(Intent.createChooser(i, "Share Image"));
}
public Uri getUri() {
if (selectedImageUri != null) {
return selectedImageUri;
} else {
return Uri.parse("android.resource://" + getPackageName() + "/" + R.mipmap.ic_launcher);
}
}
It was worked in ACTION_GET_CONTENT used URI but mipmap resource was not working in some application like Facebook and watsapp. I read from some stack answer that Image must be add in Extenrnal storage. Its not working for this URI.
Uri.parse("android.resource://" + getPackageName() + "/" + R.mipmap.ic_launcher
in what's app and Facebook and why it was working in other app like default messing app, Twitter etc.?
Try this function:
// if targetSDK >= 23, please check for runtime permission: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also add the same permission to your Manifest file.
private void shareViaWhatsApp() {
Uri imageUri = null;
try {
imageUri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(),
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher), null, null)); //You may need to check for permission for this.
} catch (NullPointerException e) {
}
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
emailIntent.putExtra(Intent.EXTRA_TEXT, "Text to share");
emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri); //............Pass Image URI here.........
emailIntent.setType("image/*");
emailIntent.setPackage("com.whatsapp");
startActivity(Intent.createChooser(emailIntent, "Share..."));
}
You can try this:
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"http://play.google.com/store/apps/details?id=" + getPackageName());
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), getBitMap(), "I am Happy", "Share happy !")));
sendIntent.setType("image/png");
sendIntent.setPackage("com.whatsapp");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(sendIntent);
} else {
Toast.makeText(getApplicationContext(), "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
startActivity(goToMarket);
}
where getBitMap() is function in which get the image bitmap which you want to share.
Use Below Code for every app in you can share image, like whats app, hike or mail and many other
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.mipmap.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing");
startActivity(Intent.createChooser(shareIntent, "Send your image"));

Image and Text not going at a time through android messaging

Tried with below code and its going image but not text through messaging and it works fine with email, whatsapp. can any one suggest me how to do it
Uri shareImageUri = saveBitmapImage(result);
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_STREAM, shareImageUri);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Hiiiiiiiiiiiiiiiiiiiiiiii");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"byeeeeeeeeeeee");
emailIntent.setType("image/png");
mActivity.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Try this code:
String fileName = "image_file_name.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 Image and Text"));
or
File filePath = getFileStreamPath("share_image.jpg"); //optional //internal storage
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "share content");
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath))); //optional//use this when you want to send an image
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "share"));

attach image from an app to gmail and share

I want to share my image from app only using gmail.
Here is my piece of code:
if (id == R.id.menu_item_share) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_EMAIL,`enter code here`
new String[] { "bipush.osti#gmail.com" });
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "compose mail");
sharingIntent.putExtra(Intent.EXTRA_STREAM, targetUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
How do I modify my codes
This method allow you to share the data for a single application.
public void share(String nameApp, String imagePath, String message) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_EMAIL,`enter code here`
new String[] { "bipush.osti#gmail.com" });
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "compose mail");
List<ResolveInfo> resInfo = getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your mime
// type
if (info.activityInfo.packageName.toLowerCase().contains(
nameApp)
|| info.activityInfo.name.toLowerCase().contains(
nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT, message);
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(imagePath)));
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
} catch (Exception e) {
Log.v("VM",
"Exception while sending image on" + nameApp + " "
+ e.getMessage());
}
}
For example you can share the image for the gmail by using.
File filePath = new File("your image path");
share("gmail",filePath.toString(),"your comment");
you can also share to other application by simply changing the name.
share("facebook",filePath.toString(),"your comment");
share("twitter",filePath.toString(),"your comment");
Try to setType message/rfc822 instead of image/png
You have to mention the package name of the application which you want to handle it. To send it through the Gmail app add the below line
sharingIntent.setPackage("com.google.android.gm");

Sending text and Image simultaneously in Android

In my application, My requirement is to send Image and text simultaneously. So I use the following code
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_TEXT, "My photos");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+f));
startActivity(Intent.createChooser(share, "Share Image"));
But only the image is sended but the text is not sending. How can I solve this problem?
plz try this
//assuming uris is a list of Uri
Intent intent = null;
if (uris.size > 1){
intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
} else if (uris.size() == 1) {
intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));}
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, "Some message");
startActivity(Intent.createChooser(intent,"compatible apps:"));
You are setting the MIME type of that Intent to image that's why only the image is sent.
Something like this will solve your problem:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("*/*");
share.putExtra(Intent.EXTRA_TEXT, "My photos");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+f));
startActivity(Intent.createChooser(share, "Share Image"));
String message= "My photos";
URI = Uri.parse("file://" + f);
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("*/*");
if (URI != null) {
share.putExtra(Intent.EXTRA_STREAM, URI);
}
share.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share, "Share Image"));
This way should be ok.

Categories

Resources