简体   繁体   English

使用Android ACTION_SEND通过Messenger和Facebook发送图像

[英]Using Android ACTION_SEND to send image via Messenger and Facebook

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). 在我的应用程序中,我希望能够使用ACTION_SEND意图通过电子邮件,Facebook或Messenger(MMS)发送保存在本地SD卡上的图片。 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". 有了我的代码,我可以成功通过电子邮件将图片作为附件发送,但当我选择Facebook时,我收到错误,“加载照片时出错”,当我尝试选择Messenger时,它说:“抱歉,你不能添加这个图片到你的留言“。

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? 有没有人知道我需要改变什么来使这个代码与Messenger和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). 要使用MMS发送图像,您必须获取媒体提供程序的URI,文件路径中的URI不起作用(之前我遇到过此问题)。 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). 对于Facebook,我不知道如何使用ACTION_SEND发送图像,因为我使用Facebook SDK发布状态和发送照片(这是一个更好的方式,因为你不需要依赖之前安装了facebook的手机)。

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();
        }
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM