2017-07-06 152 views
1

我正在將我的應用程序拍攝的照片記錄在sqlite數據庫中,我知道這不是很推薦,我需要將這些照片傳輸到統一所有應用程序的所有數據的中央數據庫。減小照片的尺寸

的辦法,我減少和寫入sqlite的銀行是這樣的:

Android.Graphics.Drawables.BitmapDrawable bd = (Android.Graphics.Drawables.BitmapDrawable)imageView.Drawable; 
Bitmap bitmap = bd.Bitmap; 
MemoryStream stream = new MemoryStream(); 
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream); 
byte[] ba = stream.ToArray(); 
string bal = Base64.EncodeToString(ba, Base64.Default); 

個人而言,我編輯我的問題,因爲這個問題是不是在爲我原以爲這可能是選擇,但在寫入數據庫時​​。上面的代碼在設備上搜索照片或圖像時效果很好。問題是當它爲應用程序拍攝照片時,我得到下面的這個錯誤。

我想我需要壓縮App.bitmap,它會立即拍攝照片。我只是不知道該怎麼做。正如我所說的,上面的代碼對設備拍攝的照片非常有用,但是當我通過我的應用程序拍攝時,出現此錯誤。

我打算髮布我的OnActivityResult方法,以便你能幫我弄清楚這個錯誤是什麼。

錯誤:

Java.Lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 

搜索這個錯誤是因爲有最大2MB的光標的大小返回在互聯網上。

OnActivityResult方法:

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 
     { 

     } 

    } 

沒有取照片時,選擇不返回上面引述的錯誤。我在指望你的幫助。

+0

你是不是降低了圖像的大小,在所有的,至少在這裏 – tyczj

+0

出多大的數據,你是實際寫入到數據庫的代碼?你真的檢查過,還是隻是在做假設? – Jason

+0

爲什麼不將照片存儲在設備存儲上,並在sqlite中存儲uri給他們? – Shmuel

回答