【问题标题】:MobileServiceInvalidOperationException: The request could not be completed. (Bad Request) on save imagesMobileServiceInvalidOperationException:无法完成请求。 (错误请求)关于保存图像
【发布时间】:2016-10-14 16:00:13
【问题描述】:

我正在做一个使用 Android 客户端和移动 Azure 服务应用程序的 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 移动服务中的项目定义:

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 移动服务:

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

            MobileServiceContext context = new MobileServiceContext();

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

...
}

【问题讨论】:

  • 您能否提供有关您的代码的更多信息?
  • 添加相关代码

标签: android azure azure-mobile-services


【解决方案1】:

您需要为此使用 Azure 存储。在这里查看我书中的食谱:https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter4/recipes/

Azure 存储没有免费选项。

【讨论】:

    猜你喜欢
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多