上传照片与mvvmcross和单触web服务(uploading photo to a webser

2019-07-17 10:53发布

我想要做的就是简单地用单触摸/单Droid和mvvmcross,希望在某种程度上将照片上传到一个web服务,所以我只需要编写一次代码为Android和IOS :)

我最初的想法是让(使用意图在android系统)获得的图像的路径,用户选择一个图像。 然后使用MvxResourceLoader资源加载,从路径打开一个流,然后用restsharp创建与流POST请求。

但是我已经打了一个壁,当用户选取的图像的路径是例如“/外部/图像/媒体/ 13”。 使用MvxResourceLoader的ResourceLoader时,在文件中此路径结果未发现异常。

任何想法,为什么我得到的异常或者是有一个更好的方法实现我的目标?

Answer 1:

这是我结束了doinging它 - 谢谢你斯图尔特和所有的链接:)

public class PhotoService :IPhotoService, IMvxServiceConsumer<IMvxPictureChooserTask>,IMvxServiceConsumer<IAppSettings>
    {
        private const int MaxPixelDimension = 300;
        private const int DefaultJpegQuality = 64;

        public void ChoosePhotoForEventItem(string EventGalleryId, string ItemId)
        { 
        this.GetService<IMvxPictureChooserTask>().ChoosePictureFromLibrary(
               MaxPixelDimension,
                DefaultJpegQuality,
               delegate(Stream stream) { UploadImage(stream,EventGalleryId,ItemId); },
                () => { /* cancel is ignored */ });

        }

        private void UploadImage(Stream stream, string EventGalleryId, string ItemId)
        {
            var settings = this.GetService<IAppSettings>();
            string url = string.Format("{0}/EventGallery/image/{1}/{2}", settings.ServiceUrl, EventGalleryId, ItemId);
            var uploadImageController = new UploadImageController(url);

            uploadImageController.OnPhotoAvailableFromWebservice +=PhotoAvailableFromWebservice;

            uploadImageController.UploadImage(stream,ItemId);

        }
    }

    public class PhotoStreamEventArgs : EventArgs
    {
        public Stream PictureStream { get; set; }

        public Action<string> OnSucessGettingPhotoFileName { get; set; }

        public string URL { get; set; }
    }

     public class UploadImageController : BaseController, IMvxServiceConsumer<IMvxResourceLoader>, IMvxServiceConsumer<IErrorReporter>, IMvxServiceConsumer<IMvxSimpleFileStoreService>
    {

        public UploadImageController(string uri)
            : base(uri)
        {

        }
        public event EventHandler<PhotoStreamEventArgs> OnPhotoAvailableFromWebservice;


        public void UploadImage(Stream stream, string name)
        {
            UploadImageStream(stream, name);
        }


        private void UploadImageStream(Stream obj, string name)
        {

            var request = new RestRequest(base.Uri, Method.POST);

            request.AddFile("photo", ReadToEnd(obj), name + ".jpg", "image/pjpeg");

            //calling server with restClient
            var restClient = new RestClient();

            try
            {

                this.ReportError("Billedet overføres", ErrorEventType.Warning);

                restClient.ExecuteAsync(request, (response) =>
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        //upload successfull
                        this.ReportError("Billedet blev overført", ErrorEventType.Warning);
                        if (OnPhotoAvailableFromWebservice != null)
                        {
                            this.OnPhotoAvailableFromWebservice(this, new PhotoStreamEventArgs() { URL = base.Uri });
                        }
                    }
                    else
                    {
                        //error ocured during upload
                        this.ReportError("Billedet kunne ikke overføres \n" + response.StatusDescription, ErrorEventType.Warning);
                    }
                });
            }
            catch (Exception e)
            {

                this.ReportError("Upload completed succesfully...", ErrorEventType.Warning);
                if (OnPhotoAvailableFromWebservice != null)
                {
                    this.OnPhotoAvailableFromWebservice(this, new PhotoStreamEventArgs() { URL = url });
                }
            }


        }

        //method for converting stream to byte[]

        public byte[] ReadToEnd(System.IO.Stream stream)
        {
            long originalPosition = stream.Position;
            stream.Position = 0;
            try
            {
                byte[] readBuffer = new byte[4096];
                int totalBytesRead = 0;
                int bytesRead;
                while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;
                    if (totalBytesRead == readBuffer.Length)
                    {

                        int nextByte = stream.ReadByte();
                        if (nextByte != -1)
                        {
                            byte[] temp = new byte[readBuffer.Length * 2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }

                    }
                }

                byte[] buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
                return buffer;
            }
            finally
            {
                stream.Position = originalPosition;
            }
        }

    }


Answer 2:

尝试:

  • 问题在WP拍摄图像,并显示他们与MvvmCross
  • 需要的例子拍照与MonoDroid的和MVVMCross
  • https://github.com/Redth/WshLst/ -使用Xam.Mobile为它的拍照


文章来源: uploading photo to a webservice with mvvmcross and mono touch