【问题标题】:xamarin.forms Storing and retrieving image files using sqlitexamarin.forms 使用 sqlite 存储和检索图像文件
【发布时间】:2016-12-24 15:12:50
【问题描述】:

目前我在 sqlite 中存储图像(使用 ImageSource 元素)。在我尝试存储之后,我正在从本地目录中读取文件 图像到 sqlite 但我无法存储图像。谁能帮我储存。还如何将图像源转换为 xamarin.forms 中的字节

这是我的代码快照

Interface
 Task<Product> AddToDo(string title, string subtitle,ImageSource img);

Calling method
ImageFile = ImageSource.FromFile("Chinese_6_600_C.jpg");

await azureService.AddToDo(Title, Subtitle, ImageFile);


public async Task<Product> AddToDo(string title, string subtitle, ImageSource img)

{

await Initialize();

 var item = new Product     
{

Title=title,

Subtitle=subtitle,

 ImageFile= img

 };


//TODO 5: Insert item into todoTable

await todoTable.InsertAsync(item);

//Synchronize todos

await SyncToDos();

return item;

 }

这里我已经导入了 nuget 包 '( Microsoft.WindowsAzure.MobileServices.SQLiteStore)' 用于在 sqlite 和 azure 云存储之间存储数据。 当我将图像源插入' (await todoTable.InsertAsync(item)'时,它不接受。请帮助我处理这种情况

【问题讨论】:

标签: c# sqlite xamarin.forms azure-sql-database


【解决方案1】:

而不是将图像(字节[])作为blob从sqlite db存储和检索,将图像转换为base64string并将图像作为字符串存储在sqlite中,同时检索将接收到的base64string从sqlite转换为图像(byte[])

Base64 转位图:

public Bitmap Base64ToBitmap(String base64String)
{
    byte[] imageAsBytes = Base64.Decode(base64String, Base64Flags.Default);
    return BitmapFactory.DecodeByteArray(imageAsBytes, 0, imageAsBytes.Length);
}

位图转Base64:

public String BitmapToBase64(Bitmap bitmap)
{
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.Compress(Bitmap.CompressFormat.Png, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.ToByteArray();
    return Base64.EncodeToString(byteArray, Base64Flags.Default);
}

【讨论】:

  • 你用的是什么位图库?
猜你喜欢
  • 1970-01-01
  • 2015-11-21
  • 2011-04-30
  • 2015-03-16
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多