Using Android ACTION_SEND to send image via Messenger and Facebook - android

In my app i want to be able to use the ACTION_SEND intent to send a picture saved on my local SD card by either Email, Facebook, or the Messenger(MMS). With the code I have I can sucessfully email the picture as an attachment but when I select Facebook i get the error, "An error occured while loading the photo" and when i try selecting the Messenger it says, "Sorry, You cannot add this picture to your message".
Here is my code:
File pic = new File(Environment.getExternalStorageDirectory()+ File.separator + "images" + File.separator + "picture.jpg");
Uri pngUri = Uri.fromFile(pic);
Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);
picMessageIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
picMessageIntent.setType("image/jpeg");
picMessageIntent.putExtra(Intent.EXTRA_STREAM, pngUri);
startActivity(Intent.createChooser(picMessageIntent, "Send your picture using:"));
Does anyone have any idea what i need to change to make this code work with the Messenger and Facebook?

To send image with MMS, you must get the URI of media provider, URI from file path doesn't work (I met this problem before). For Facebook, I've no idea how to user ACTION_SEND to send image, since I used Facebook SDK to post status and send photo (and that is a much better way since you don't need to rely on phones which have facebook installed before).
protected void sendMMS(final String body, final String imagePath) {
MediaScannerConnectionClient mediaScannerClient = new MediaScannerConnectionClient() {
private MediaScannerConnection msc = null;
{
msc = new MediaScannerConnection(getApplicationContext(), this);
msc.connect();
}
public void onMediaScannerConnected() {
msc.scanFile(imagePath, null);
}
public void onScanCompleted(String path, Uri uri) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("sms_body", body);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("image/png");
startActivity(sendIntent);
msc.disconnect();
}
};
}

Related

Android: Only show email apps OR display a message to user for Intent.ACTION_SEND when attaching a file

Right now for sdk 30, I am successfully able to send and attach a json file to some email client when using ACTION_SEND. However there are two issues that I noticed when using ACTION_SEND:
it shows ALL apps, not just email apps, which is what I want
it does NOT show the intent with title/text for the user to choose an email app. I do NOT just want to use a Toast message
I think this can be solved using ACTION_SENDTO instead, but then I won't be able to attach my file, so that actually won't work.
With this said, I would like to solve either point 1 or 2 (preferably point 1), if not both.
Here is what I have:
private void sendToEmail(String emailAddress, String filePath) {
File file = new File(filePath);
file.setReadOnly();
String[] to = { emailAddress };
Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", file);
grantUriPermission("com.my.package", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent = new Intent(Intent.ACTION_SEND);
//intent.setData(Uri.parse("mailto:")); // this does NOT work for the above action type
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, "Your backup file");
intent.putExtra(Intent.EXTRA_TEXT, "Your data for your backup is attached to the file " + BACKUP_NAME + ".");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("message/rfc822");
// this does NOT create the below title for ACTION_SEND, as per documentation for createChooser()
Intent chooser = Intent.createChooser(intent, "Pick an email app to send attachment: " + BACKUP_NAME);
if (chooser.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
}

Xamarin Android Sharing Intent under Marshmallow

In my Application I try to share an Image with the Intent (Intent.ActionSend).
For the guaranteed Access I use a Fileprovider which creates the Uri to the file.
Under Android Nougat my code works but with Marshmallow not. It shows no errors but the Image is not sent to the recieving App.
The Output from VisualStudio also shows no errors or somthing like that.
I couldn't find any answer online so far. What is the problem with Marshmallow?
public void SendPhoto(IEnumerable<string> photos, string text)
{
var files = new List<Android.Net.Uri>();
//creates for every photo an own ContentURI
foreach (var path in photos)
{
var file = new Java.IO.File(path);
files.Add(FileProvider.GetUriForFile(Context, "com.testapp.fileprovider", file));
}
//Checks if Photolist are empty
if (!files.Any())
throw new Exception("Photos not found");
Intent intent = new Intent(Intent.ActionSend);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
intent.SetType("image/jpeg");
intent.PutExtra(Intent.ExtraSubject, "Subject");
intent.PutExtra(Intent.ExtraStream, files.First());
//creates the sharemessage
var shareMessage = files.Count == 1 ? "Send photo from TestApp" : "Send photos from TestApp";
//start Intent chooser
Context.StartActivity(Intent.CreateChooser(intent, shareMessage));
}

Stray file path added to email on intent using FileProvider attachment

I've got an intent that uses FileProvider to read from internal file storage for my app to send a file via email (or similar apps). The code works great everywhere apparently except for Gmail, which strangely adds a version of the provider path itself to the list of addressees of the email.
This is the code generating the intent:
public static Intent getSendIntent(Uri uri) {
final Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setDataAndType(uri, "message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "my#email.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "Export");
i.putExtra(Intent.EXTRA_TEXT, "See the attached...");
i.putExtra(Intent.EXTRA_STREAM, uri);
return i;
}
This is the code to start the activity:
File file = new File(getFilesDir(), filename);
Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.myapplication.provider", file);
Intent i = Utils.getSendIntent(uri);
try {
startActivity(Intent.createChooser(i, "Send file..."));
} catch (Exception e) {
Log.d(TAG, "failed to start send activity: " + e.getMessage());
Toast.makeText(MainActivity.this, "No suitable activity found for export.", Toast.LENGTH_SHORT).show();
}
Works well in Slack, Evernote, etc. but in Gmail in addition to the email address I've provided, another addressee in this kind of format is added:
//com.example.myapplication.provider/my_files/filefile.csv
which prevents the email from sending until it's manually removed from the message. Everything else about the message is as expected.
Any clue how to prevent this?
The following Captain Obvious solution resolved the problem.
Replace:
i.setDataAndType(uri, "message/rfc822");
with:
i.setType("message/rfc822");
since, as #CommonsWare's question inferred, the file content isn't itself in rfc822 format.

File sharing using Intent.ACTION_SEND gives access denied on BBM

I am trying to share an audio file saved by my application. The file is added to the media store so all apps can access it.
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(finalFile);
mediaScannerIntent.setData(fileContentUri);
this.sendBroadcast(mediaScannerIntent);
I use Intent.ACTION_SEND to share the files to other apps:
public void shareRecording(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("audio/mp3");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + recording.getFilePath()));
try {
startActivity(Intent.createChooser(i, "Share " + recording.getName()));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no app installed to share your audio file.", Toast.LENGTH_SHORT).show();
}
}
All working good with all apps like GMail, Yahoo mail, Whatsapp, ...
except BBM, it gives access denied.
What do I need to do to make it work on BBM ?
Thanks for any help.
UPDATE:
I used
Uri fileUri = Uri.parse("content://" + recording.getFilePath());
instead of
Uri fileUri = Uri.parse("file:///" + recording.getFilePath());
and it worked on BBM but not othe apps
So what is the difference between havin the URI parsed with: "file:///" and "content://" ?
And how can I used this to get the share working for all apps ?
The solution is to use the actual file to initialize the Uri object.
File recordingFile = new File(recording.getFilePath());
Uri fileUri = Uri.fromFile(recordingFile);
This worked with all the apps that can share a file.

photo share intent in android

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");
Uri uri = Uri.parse(pathToImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
i was used above code for sharing image on social sites.when i posting image on facebook only text is posted and image is not coming.how can we get the image and pathtoimage is string variable i am getting sever image path and stored in string variable.but it is not working.please give any solutions for my above question.?
Try this
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
Refer more links
http://android-developers.blogspot.in/2012/02/share-with-intents.html
Android Share Via Dialog
Image post into facebook wall from android sdcard
This is the solution I came up with:
This function allows you to share to instagram, facebook or any other app if you know the package name. If the app is not installed it redirects to the Play Store. If you send "others" as parameters, you will display a list of apps that also support image sharing.
shareImageIntent(mActivity,"image/*", "/storage/emulated/0/Pictures/Instagram/IMG_20150120_095603.jpg", "Instagram Sharing test. #Josh","com.instagram.android"); // parameters - for facebook: "com.facebook.katana" , to display list of other apps you can share to: "others"
public void shareImageIntent(Activity a,String type, String mediaPath, String caption, String app){
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(app);
if ((intent != null)||(app.equals("others"))){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
if(!app.equals("others"))
share.setPackage(app);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, caption);
// Broadcast the Intent.
//startActivity(Intent.createChooser(share, "Share to"));
a.startActivity(share);
}
else{
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
intent.setData(Uri.parse("market://details?id="+app));
a.startActivity(intent);
}
}
You can not use Intent to post to Facebook. They have specifically blocked this. You have to either use their SDK or copy it to the clipboard and have the user paste it after the facebook interface opens. I am having this same problem.

Categories

Resources