【问题标题】:Image downloaded from Azure Storage not being displayed从 Azure 存储下载的图像未显示
【发布时间】:2016-06-01 03:10:59
【问题描述】:

我是 Xamarin 的新手。我正在尝试显示下载图像的列表。我正在从 Azure 上的 APP API 下载图像,我将文件存储在 Azure 存储中。

我的服务器代码如下:

public HttpResponseMessage Get(string PK, string RK)
{
    //Creating CloudBlockBlolb...

    byte[] bytes = new byte[blockBlob.Properties.Length]

    for(int i = 0; i < blockBlob.Properties.Length; i++){
        bytes[i] = 0x20;
    }

    blockBlob.DownloadToByteArray(bytes, 0);

    HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK);
    resp.Content = new ByteArrayContent(bytes);
    resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpg");
    return resp;
}

我的 Xamarin 代码如下:

public MainPage ()
{
    //...

    List<PicturePost> list = new List<PicturePost>{
         new PicturePost("title", "subtitle", "link/api/Pictures?PK=xxx&RK=yyy")
    };

    InitializeComponent ();
    listView.ItemTemplate = new DataTemplate (typeof(CustomImageCell));
    listView.HasUnevenRows = true;
    listView.ItemsSource = list;

    //...
}

这里是CustomImageCell的相关代码:

var image = new Image ();
image.SetBinding (Image.SourceProperty, "image");
//...
horizontalLayout.Children.Add (image);

我知道我的 API 调用有效,因为当我在浏览器上测试它时,它会返回图像。我也知道,如果我使用任何随机链接,例如http://www.natureasia.com/common/img/splash/thailand.jpg,图像就会被下载并正确显示。只有当我使用 API 链接时,它似乎不起作用。谁能告诉我我做错了什么?

【问题讨论】:

  • 容器或 blob 是否设置为公共?你能调试一下,看看你是否得到403(禁止)?如果是这种情况,要么传递存储密钥以进行身份​​验证,要么将容器/blob 更改为公共以便从任何地方访问它
  • @BrunoFaria 这是私人的。我将它设置为公开,它并没有改变任何东西。不过,现在想想。当我通过 API 调用时,我仍然能够访问图像。我猜是因为我通过 API 传递,我不需要将容器设置为公共。
  • 是的。这是正确的。如果您通过请求传递访问令牌,则不需要。

标签: azure xamarin xamarin.forms


【解决方案1】:

所以在我的公共 MainPage() 中,我添加了以下内容:

listView.BeginRefresh ();
listView.EndRefresh ();

我意识到在某些时候下载图片需要一些时间。我假设在创建 listView 时,图像还没有完成下载,所以我添加了上面的代码......很确定这不是最好的方法(可能 await 会更好,但我不知道哪里)。

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 2018-08-03
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多