2017-04-25 145 views
1

在我的類我有一個方法,搜索圖片庫中的圖片,也接收從手機相機拍攝的圖像,我現在需要保存此圖像在sqlite數據庫。我正在使用像BLOB這樣的數據庫字段,但不喜歡在bity []中序列化圖像或在decode64中轉換以寫入數據庫。如何將圖像保存到sqlite數據庫

我想知道如果任何人有任何實施例來傳遞xaramin機器人我張貼接收圖像的方法

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

      // Make it available in the gallery 
      try 
      { 
       Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); 
       Uri contentUri = Uri.FromFile(App._file); 
       mediaScanIntent.SetData(contentUri); 
       SendBroadcast(mediaScanIntent); 

       // Display in ImageView. We will resize the bitmap to fit the display. 
       // Loading the full sized image will consume to much memory 
       // and cause the application to crash. 

       int height = Resources.DisplayMetrics.HeightPixels; 
       int width = imageView.Height; 
       App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height); 
       if (App.bitmap != null) 
       { 
        imageView.SetImageBitmap(App.bitmap); 
        App.bitmap = null; 

       } 

       // Dispose of the Java side bitmap. 
       GC.Collect(); 
      } 
      catch 
      { 


      } 
      try 
      { 
       if (resultCode == Result.Ok) 
       { 
        var imageView = 
         FindViewById<ImageView>(Resource.Id.imageView1); 
         imageView.SetImageURI(data.Data); 

       } 
      } 
      catch { 

      } 

     } 
+1

那你嘗試保存在數據庫中的項目? –

回答

0

代替存儲&檢索圖像(字節[])作爲從源碼斑點分貝轉換圖像base64string和存儲的圖像作爲在源碼串和從源碼而檢索轉換接收base64string到圖像(字節[])

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); 
} 
+0

Jay 對不起,延遲,但我怎樣才能通過此功能的圖像領域,並插入? – mba

+0

是的,你可以通過圖像 –

+0

但我該怎麼做? – mba