MobileServiceInvalidOperationException:无法完成请求。 (错误请求)保存图片

时间:2016-10-14 16:00:14

标签: android azure azure-mobile-services

我正在做一个使用Android客户端和Mobile Azure Service应用程序的POC应用程序。

我设法实现了一个使用Sql server Compact 4.0数据库文件的解决方案,以便存储一些文本和小图像(计划每个最大300 Kb)。

然而,它仅适用于以Jpeg格式压缩且质量最低的非常小的图像(例如2 Kb)

(使用此:image.Compress(Bitmap.CompressFormat.Jpeg, 0, stream);)。

我确实得到了“ Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException:请求无法完成。(错误请求) ”尝试保存项目时更大图片的错误使用该应用程序。我没有在响应中找到任何关于请求长度限制的确切提及,我假设这一点。

所以我的问题是 - Azure存储是完成此任务的唯一方法吗? (另外,不确定使用Azure存储的任何免费选项)

内容上传的UI部分(我正在使用Xamarin)

选择要上传的图像(进入_currentImage变量):

protected override void OnActivityResult(int requestCode, Result result, Intent data)
        {
            base.OnActivityResult(requestCode, result, data);

            if (requestCode == 1)
            {
                if (result == Result.Ok)
                {
                    var selectedImage = data.Data;

                    var filePath = GetPathToImage(selectedImage);

                    var photo = BitmapFactory.DecodeFile(filePath);

                    _currentImage = photo;

                    ImagePreview.SetImageBitmap(photo);
                }
            }
        }

保存调用Azure移动应用的内容:

private async void UploadItemContent()
        {
            using (MobileServiceClient client = new MobileServiceClient(Configuration.Urls.CloudAppUrl))
            {
                var table = client.GetTable<Item>();

                var content = ContentDescriptionEditText.Text;
                var header = ContentHeaderEditText.Text;

                var stream = new MemoryStream();
                _currentImage.Compress(Bitmap.CompressFormat.Jpeg, 0, stream);
                var bitmapData = stream.ToArray();

                var item = new Item
                {
                    Content = content,
                    Header = header,
                    Image = bitmapData,
                    CreationDate = DateTime.Now.ToUniversalTime()
                };

                try
                {
                    await table.InsertAsync(item);

                    ShowSuccessStatus();
                }
                catch (Exception ex)
                {
                    ShowError(ex.Message);
                }
            }
        }

后端

Azure Mobile服务中的项目定义:

public class Item : ITableData
    {
        public Item()
        {
        }

        [Key]
        [TableColumn(TableColumnType.Id)]
        public string Id { get; set; }

        public string Header { get; set; }

        public string Content { get; set; }

        [Column(TypeName = "image")]
        public byte[] Image { get; set; }

        public DateTime? CreationDate { get; set; }

        public DateTime? UpdateDate { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        [Index(IsClustered = true)]
        [TableColumn(TableColumnType.CreatedAt)]
        public DateTime? CreatedAt { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        [TableColumn(TableColumnType.UpdatedAt)]
        public DateTime? UpdatedAt { get; set; }

        [TableColumn(TableColumnType.Deleted)]
        public bool Deleted { get; set; }

        [TableColumn(TableColumnType.Version)]
        [Timestamp]
        public byte[] Version { get; set; }

        [NotMapped]
        DateTimeOffset? ITableData.CreatedAt
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        [NotMapped]
        DateTimeOffset? ITableData.UpdatedAt
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }
    }

ItemController进入Azure Mobile服务:

public class ItemController : TableController<Item>
    {
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);

            MobileServiceContext context = new MobileServiceContext();

            DomainManager = new EntityDomainManager<Item>(context, Request);
        }

...
}

1 个答案:

答案 0 :(得分:0)

您需要使用Azure存储。请参阅我的书中的食谱:https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter4/recipes/

Azure存储没有免费选项。