减小照片的尺寸
我正在将我的应用程序拍摄的照片记录在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
{
}
}
没有取照片时,选择不返回上面引述的错误。我在指望你的帮助。
使用EncoderParameter指定图像的压缩级别。 看到https://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameter(v=vs.110).aspx 和https://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder.quality(v=vs.110).aspx
个人感谢大家的关注和帮助我的企图,我设法通过传递高度和宽度的大小来压缩问题来解决问题。谢谢你们 – mba
你是不是降低了图像的大小,在所有的,至少在这里 – tyczj
出多大的数据,你是实际写入到数据库的代码?你真的检查过,还是只是在做假设? – Jason
为什么不将照片存储在设备存储上,并在sqlite中存储uri给他们? – Shmuel