Sharing image from android app

走远了吗. 提交于 2019-12-11 02:48:31

问题


I am trying to share an image from my Android app. I am trying to send it as an Email attachment as well as a photo on WhatsApp.

The code is:

String imageUrl = Path to image (eg. sdcard/pictures/image1.jpg);
shareImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri uriToImage= Uri.parse(imageUrl);
                    Log.d("Image", ""+uriToImage);
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
                    shareIntent.setType("image/*");
                    startActivity(Intent.createChooser(shareIntent, "Share image:"));
                }
            });

What's happening is:

  1. On WhatsApp I can share the image easily.
  2. On Gmail, it says that the attachment could not be sent.
  3. On Hangouts I get a toast which says Photo couldn't be found
  4. On Facebook too, the post is not accompanied with the Image but I can post.
  5. On Facebook Messenger, it crashes without opening.

The tutorial that I have followed for this is given here. The send binary content part of the tutorial is the one that I have implemented.

Another thing I tried was to set the image in an ImageView and see if it is displayed. The image is displayed correctly. Also, the log message prints the correct path of the image.

I also read and tried the answers to: Question 1 and Question 2 but to no avail.

Where am I going wrong?


回答1:


try this,

try
                {
                    File myFile = new File(share_image_path);
                    MimeTypeMap mime = MimeTypeMap.getSingleton();
                    String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
                    String type = mime.getMimeTypeFromExtension(ext);
                    Intent sharingIntent = new Intent("android.intent.action.SEND");
                    sharingIntent.setType(type);
                    sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
                    startActivity(Intent.createChooser(sharingIntent, "Share using"));
                }
                catch (Exception e)
                {
                    Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }



回答2:


Yes the @CommonsWare Answer is correct you missed the Schema in your Intent.putExtra() and that's why it is failing to read your image in other social media platform Here's my solution

 Uri fileUri = Uri.fromFile(new File(imagePath));

 //No need to do mimeType work or ext 

 Intent intent = new Intent(Intent.ACTION_SEND);
 intent.putExtra(Intent.EXTRA_STREAM, fileUri);
 intent.setType("image/*");
 startActivity(Intent.createChooser(intent, "Share Image:"));

BTW it works on all the mentioned platform

   Uri image_uri = Uri.parse("file://"+imagePath);

Another method of creating Uri and passing it to intent



来源:https://stackoverflow.com/questions/29629205/sharing-image-from-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!