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_S开发者_开发技巧END);
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();
}
};
}
精彩评论