图片缩略图已保存,而不是MediaStore.ActionImageCapture中的完整图片 [英] Image thumbnail being saved instead of full-image from MediaStore.ActionImageCapture
问题描述
我目前正在尝试通过android拍照,并将其保存在以后将其上传到数据库的位置.在在线上阅读了一些教程之后,我发现我使用的代码仅保存所捕获图像的低分辨率缩略图,而不是完整图像.
I am currently trying to take a picture through android, and save the image where I will upload it to a database later. After following some tutorials online, I have found that the code I am using only saves a low-resolution thumbnail of the image I am capturing instead of the full image.
是否可以获取完整尺寸的图像进行保存?由于使用数据库的软件的设置方式,因此格式必须为Jpeg.
Is there a way to get the full sized image for saving? The format needs to be Jpeg due to the way the software that uses the database is setup.
按预期方式拍摄照片:
private void _openCamera_Click(object sender, EventArgs e)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
}
这是图像最终被保存为缩略图的地方.理想情况下,此部分将是我们修改的唯一代码:
This is where the image ends up being saved as a thumbnail. Ideally this section would be the only code we modify:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.Extras.Get("data");
this._photo.SetImageBitmap(bitmap);
MemoryStream memStream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, memStream);
this._tempImageData = memStream.ToArray();
}
更新:SushiHangover的响应完美无缺.为了处理缓存的图像,我使用了以下代码:
Update: SushiHangover's response works flawlessly. To work with the cached image I used the following code:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode != Result.Ok || requestCode != 88)
{
return;
}
Bitmap bitmap = BitmapFactory.DecodeFile(cacheName);
this._photo.SetImageBitmap(bitmap);
MemoryStream memStream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, memStream);
this._tempImageData = memStream.ToArray();
}
推荐答案
这是正式版Android Photo Basics的真正简化的 C#版本,用于全尺寸照片.
This is a really simplified C# version of the official Android Photo Basics for a full-size photo.
注意:这会将完整尺寸的照片保存在应用程序的沙盒 cache
目录
Note: This saves the full size photo in the app's sandboxed cache
directory
<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="cache_images" path="." />
</paths>
在清单的应用
打开/关闭标签内添加文件提供程序
Add a FileProvider within the manifest's application
open/close tags:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
创建照片文件并请求照片应用:
cacheName = Path.Combine(CacheDir.AbsolutePath, Path.GetTempFileName());
using (var cacheFile = new Java.IO.File(cacheName))
using (var photoURI = FileProvider.GetUriForFile(this, PackageName + ".fileprovider", cacheFile))
using (var intent = new Intent(MediaStore.ActionImageCapture))
{
intent.PutExtra(MediaStore.ExtraOutput, photoURI);
StartActivityForResult(intent, 88);
}
注意: cacheName
是一个类级别的变量,在OnActivityResult方法中将需要它
Note: cacheName
is a class level variable, it will be needed in the OnActivityResult method
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
if (resultCode == Result.Ok && requestCode == 88)
{
// Do something with your photo...
Log.Debug("SO", cacheName );
}
}
这篇关于图片缩略图已保存,而不是MediaStore.ActionImageCapture中的完整图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!