Mono for Android 拍照和本地图片选择上传到服务器
//添加图片
public void btn_user_idcart_click(object sender, EventArgs e)
{
//Intent intent = new Intent(Intent.ActionPick, null);
//intent.SetDataAndType(MediaStore.Images.Media.ExternalContentUri, "image/*");
Intent intent = new Intent();
intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(intent, PHOTOZOOM);
}
//拍照
public void btn_user_eidcart_click(object sender, EventArgs e)
{
var intent = new Intent(MediaStore.ActionImageCapture);
var availableActivities = this.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
if (availableActivities != null && availableActivities.Count > 0)
{
var dir = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.Path, "exuezhe");
if (!dir.Exists())
{
dir.Mkdirs();
}
_file = new Java.IO.File(dir, user.uname + ".jpg");
intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(_file));
StartActivityForResult(intent, PHOTOHRAPH);
}
}
//回调函数
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTOZOOM)
{
if (data != null)
{
StartPhotoZoom(data.Data);
}
//Android.Net.Uri uri = data.Data;
//ContentResolver cr = this.ContentResolver;
//Bitmap bitmap = BitmapFactory.DecodeStream(cr.OpenInputStream(uri));
//MemoryStream stream = new MemoryStream();
//bitmap.Compress(Bitmap.CompressFormat.Png, 50, stream);
//byte[] buffer = stream.ToArray();
//path = uapi.ModifyUserAva(buffer);
}
if (requestCode == PHOTOHRAPH)
{
if (_file != null)
{
StartPhotoZoom(Android.Net.Uri.FromFile(_file));
}
//long len = _file.Length();
//FileStream fs = new FileStream(_file.Path, FileMode.Open);
//byte[] buffer = new byte[len];
//fs.Read(buffer, 0, (int)len);
//fs.Close();
//path = uapi.ModifyUserAva(buffer);
}
if (requestCode == PHOTORESOULT)
{
if (data != null)
{
SetPicToView(data);
}
}
}
//裁剪图片
public void StartPhotoZoom(Android.Net.Uri uri)
{
Intent intent = new Intent("com.android.camera.action.CROP");
intent.SetDataAndType(uri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.PutExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.PutExtra("aspectX", 1);
intent.PutExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.PutExtra("outputX", 150);
intent.PutExtra("outputY", 150);
intent.PutExtra("return-data", true);
StartActivityForResult(intent, PHOTORESOULT);
}
//提交服务器
private void SetPicToView(Intent picdata)
{
Bitmap bitmap=null;
Bundle extras = picdata.Extras;
if (extras != null)
{
bitmap =(Bitmap)extras.GetParcelable("data");
}
ContentResolver cr = this.ContentResolver;
//Bitmap bitmap = BitmapFactory.DecodeStream(cr.OpenInputStream(uri));
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 50, stream);
byte[] buffer = stream.ToArray();
//上次buffer
//path = uapi.ModifyUserAva(buffer);
}
因为一无所有,所以无所畏惧。
不是什么东西,人出生就懂的。
——蚁族的奋斗
浙公网安备 33010602011771号