Home c#: Upload a photo to server from gallery xamarin
Reply: 0

c#: Upload a photo to server from gallery xamarin

valeria
1#
valeria Published in 2018-01-12 23:20:24Z

I'm trying to upload a photo from the gallery to a server. I have a code to pick a photo and it works but when I try to upload I get this error:

 System.NullReferenceException: Object reference not set to an instance
 of an object.   at Android.Runtime.InputStreamInvoker.Read
 (System.Byte[] buffer, System.Int32 offset, System.Int32 count)

I think the error is in what's returning when I select the photo but I'm not sure.

To pick the photo I'm using the code I found here using dependency services.

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/photo-picker/

On the main activity:

 public readonly int PickImageId = 1000;
 public TaskCompletionSource<System.IO.Stream> PickImageTaskCompletionSource { set; get; }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
    {

        base.OnActivityResult(requestCode, resultCode, intent);

        if (requestCode == PickImageId)
        {
            if ((resultCode == Result.Ok) && (intent != null))
            {
                Android.Net.Uri uri = intent.Data;
               System.IO.Stream stream = ContentResolver.OpenInputStream(uri);

                // Set the Stream as the completion of the Task
                PickImageTaskCompletionSource.SetResult(stream);
            }
            else
            {
                PickImageTaskCompletionSource.SetResult(null);
            }
        }
    }

The class with the method:

 public Task<Stream> GetImageStreamAsync()
        {
            // Define the Intent for getting images
            Intent intent = new Intent();
            intent.SetType("image/*");
            intent.SetAction(Intent.ActionGetContent);

        // Get the MainActivity instance
        MainActivity activity = Forms.Context as MainActivity;

        // Start the picture-picker activity (resumes in MainActivity.cs)
        activity.StartActivityForResult(
            Intent.CreateChooser(intent, "Select Picture"),
            activity.PickImageId);

        // Save the TaskCompletionSource object as a MainActivity property
        activity.PickImageTaskCompletionSource = new TaskCompletionSource<Stream>();

        // Return Task object
        return activity.PickImageTaskCompletionSource.Task;
    }

In the shared code:

  private Stream photo;  

    private async void btnSelect_Clicked(object sender, EventArgs e)
    {
        this.IsEnabled = false;
        photo = await DependencyService.Get<IPhotoPicker>().GetImageStreamAsync();
        if (photo!= null)
        {
           //previews the image
            image1.Source = ImageSource.FromStream(() => photo); 
        }

    }

And then to upload it I have this code:

            var content = new MultipartFormDataContent();
            content.Add(new StreamContent(photo), "\"file\"", "\"fileName.jpg\"");

            var httpClient = new System.Net.Http.HttpClient();
            var url = "http://www.mywebsite.com/Images/";
            var responseMsg = await httpClient.PostAsync(url, content);

            var remotePath = await responseMsg.Content.ReadAsStringAsync();

I'm working with andorid btw.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.333484 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO