Xamarin保存图像并在导航栏中显示

时间:2017-10-19 17:58:35

标签: c# xamarin xamarin.android

我正在尝试将图像保存到Android设备的内部存储中。然后我想在共享项目中访问导航栏中的图标。图像从API返回。它给出了Android.Content.Res.Resources + NotFoundException资源ID#0x0。

保存图片:

public async Task<bool> SaveImage(string filename, ImageSource img)
    {
        System.IO.Stream outputStream = null;
        bool success = false;

        var renderer = GetHandler(img);
        Bitmap photo = Task.Run(async () => await renderer.LoadImageAsync(img, Forms.Context)).Result;

        string savedImageFilename = System.IO.Path.Combine(documentsPath, filename);

        using (outputStream = new System.IO.FileStream(savedImageFilename, FileMode.Create))
        {
            if (System.IO.Path.GetExtension(filename).ToLower() == ".png")
                success = await photo.CompressAsync(Bitmap.CompressFormat.Png, 100, outputStream);
            else
                success = await photo.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, outputStream);
        }

        return success;
    }

加载图片:

    public FileImageSource LoadImage(string filename)
    {
        var filePath = Path.Combine(documentsPath, MyBlueAssets);
        filePath = Path.Combine(filePath, filename);
        FileImageSource image = (FileImageSource)ImageSource.FromFile(filePath);

        return image;
    }

访问和显示共享项目:

DependencyService.Get<IImageCacheDependency>().SaveImage(imageName, Image, count);
FileImageSource loadedImage = DependencyService.Get<IImageCacheDependency ().LoadImage(imageName);

navigation.Icon =  loadedImage;

0 个答案:

没有答案