Forum Xamarin Xamarin.Android

Sharing Image from app results in no image showing, and IO Error in Outlook for Android

I'm attempting to create a function which allow me to share be able to share an image through androids share dialogue, into any supporting application eg. Slack, Outlook, etc.

However when I run the code below which completes without error the image does not appear in any application I share to and Outlook fro android gives a generic IO error.

async void shareMetricFromURL(){
                WebClient webClient = new WebClient ();
                var url = new Uri ("https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/500px-Stack_Overflow_logo.svg.png");
                byte[] bytes = null;


                try{
                    bytes = await webClient.DownloadDataTaskAsync(url);
                }
                catch(TaskCanceledException){
                    return;
                }
                catch(Exception e){
                    return;
                }

                string documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString();
                string localFilename = "downloaded.png";
                string localPath = System.IO.Path.Combine (documentsPath, localFilename);

                //Sive the Image using writeAsync
                FileStream fs = new FileStream (localPath, FileMode.OpenOrCreate);
                await fs.WriteAsync (bytes, 0, bytes.Length);

                fs.Close ();

                var sendIntent = new Intent();
                sendIntent.SetAction(Intent.ActionSend);

                var uri = Android.Net.Uri.FromFile(new Java.IO.File(localPath));
                sendIntent.PutExtra(Intent.ExtraStream, uri);

                sendIntent.SetType("image/*");
                StartActivityForResult(Intent.CreateChooser(sendIntent, "Share With..."), 0);
    }

Any thoughts?

Answers

  • SHIVANKUCHAUHANSHIVANKUCHAUHAN USMember ✭✭✭

    @MatthewAllen.9962 I was able to share the image, make sure that you have device with valid internet connection and have following user permissions in AndroidManifest.xml file



  • @SHIVANKUCHAUHAN I'm unable to see the permission you stated in your post, could you post it again?Tagged: intent storage file path sharing

  • SHIVANKUCHAUHANSHIVANKUCHAUHAN USMember ✭✭✭
    edited February 2017

    @MatthewAllen.9962

    Add these permissions:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Sign In or Register to comment.