Bandini
Bandini

Reputation: 31

Trying to upload an image on imgur in c# UWP

I'm new to C# and i am doing an app using the imgurAPI. I'm trying to upload an image on it but it seems that there is a problem while reading the file. Don't know really what to do. (the error don't come from Oauth, or access token etc)

Here are the functiosn that i use. The first one is the one called when clicking on the uploading button.

public async void FileNameButton_Click()
    {
        try
        {
            Debug.WriteLine("ICI");
            FileOpenPicker fileOpenPicker = new FileOpenPicker();
            fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
            fileOpenPicker.FileTypeFilter.Add(".png");
            fileOpenPicker.FileTypeFilter.Add(".jpeg");
            fileOpenPicker.FileTypeFilter.Add(".jpg");
            fileOpenPicker.FileTypeFilter.Add(".gif");

            StorageFile file = await fileOpenPicker.PickSingleFileAsync();
            if (file != null)
            {

                _path = file.Path;
                await UploadImage();
                Debug.WriteLine(file.Path);
            }
        }
        catch (Exception exception)
        {
            Debug.WriteLine(exception.Message);
        }
    }

_

public async Task UploadImage()
    {
        Debug.WriteLine("UPLOADIMAGE");
        try
        {
            if (_path != null)
            {
                Debug.WriteLine("String to upload " + _path); 
                //_path is ok here, form is         "C:\Users\MyName\Pictures\1412091183-dreamfall-chapters.jpg"

                var endpoint = new ImageEndpoint(SimpleIoc.Default.GetInstance<ImgurApi>()._Client);
                try
                {
                    var file = File.ReadAllBytes(_path);
                    var image = await endpoint.UploadImageBinaryAsync(file);
                }
                catch (Exception message)
                {
                    Debug.WriteLine(message);
                }

            }
        }
        catch (Imgur.API.ImgurException imgurEx)
        {
            Debug.Write("An error occurred uploading an image to Imgur.");
            Debug.Write(imgurEx.Message);
        }
    }

Here are the errors i get :

Exception thrown: 'System.InvalidOperationException' in System.IO.FileSystem.dll System.InvalidOperationException: Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run. at System.IO.WinRTFileSystem.EnsureBackgroundThread() at System.IO.WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.File.InternalReadAllBytes(String path) at Epicture.ViewModels.MainPageVm.d__19.MoveNext() C:\Users\Bandini\Pictures\1412091183-dreamfall-chapters.jpg

Thanks in advance.

Upvotes: 0

Views: 338

Answers (1)

Oleh Nechytailo
Oleh Nechytailo

Reputation: 2195

You're using File.ReadAllBytes, which is a synchronous call and will block UI thread.

You should use aync IO APIs for this.

Also ImageEndpoint has a method that accepts stream, which is preferable.

example:

using(var file = File.OpenRead(_path))
{
    var image = await endpoint.UploadImageStreamAsync(file);
}

Upvotes: 0

Related Questions