2 回答
TA贡献1934条经验 获得超2个赞
我写了一个关于你的需求的演示。我添加了相机和外部存储权限并将图像存储到画廊,这里正在运行 GIF。
https://i.stack.imgur.com/FPLUY.gif
有运行demo的代码。我添加了运行时权限(相机和WriteExternalStorage)请求。并判断用户没有拍照的情况,然后返回应用程序。
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity, ActivityCompat.IOnRequestPermissionsResultCallback
{
Button button1;
ImageView imageView1;
View layout;
static readonly int REQUEST_CAMERA_WriteExternalStorage = 0;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
layout = FindViewById<RelativeLayout>(Resource.Id.sample_main_layout);
button1 = FindViewById<Button>(Resource.Id.button1);
imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);
button1.Click += (o, e) =>
{
CheckPermission();
};
}
public void CheckPermission()
{
if ((ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == (int)Permission.Granted)&& (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == (int)Permission.Granted))
{
// Camera and store permission has been granted
ShowCamera();
}
else
{
// Camera and store permission has not been granted
RequestPermission();
}
}
private void RequestPermission()
{
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.Camera, Manifest.Permission.WriteExternalStorage }, REQUEST_CAMERA_WriteExternalStorage);
}
//get result of persmissions
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
if (requestCode == REQUEST_CAMERA_WriteExternalStorage)
{
if ( PermissionUtil.VerifyPermissions(grantResults))
{
// All required permissions have been granted, display Camera.
ShowCamera();
}
else
{
// permissions did not grant, push up a snackbar to notificate USERS
Snackbar.Make(layout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show();
}
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void ShowCamera()
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Bitmap bitmap=null;
//If user did not take a photeo , he will get result of bitmap, it is null
try {
bitmap = (Bitmap)data.Extras.Get("data");
} catch(Exception e)
{
Log.Error("TakePhotoDemo1", e.Message);
Toast.MakeText(this, "You did not take a photo", ToastLength.Short).Show();
}
if (bitmap != null)
{
MediaStore.Images.Media.InsertImage(ContentResolver, bitmap, "screen", "shot");
imageView1.SetImageBitmap(bitmap);
}
else
{
Toast.MakeText(this, "You did not take a photo", ToastLength.Short).Show();
}
}
}
PermissionUtil.cs通过验证给定数组中的每个条目是否具有值 Permission.Granted,检查是否已授予所有给定权限。
public abstract class PermissionUtil
{
public static bool VerifyPermissions(Permission[] grantResults)
{
// At least one result must be checked.
if (grantResults.Length < 1)
return false;
// Verify that each required permission has been granted, otherwise return false.
foreach (Permission result in grantResults)
{
if (result != Permission.Granted)
{
return false;
}
}
return true;
}
}
有我的代码。
https://github.com/851265601/TakePhotoDemo1
MediaStore.Images.Media.InsertImage前面的代码将在 gallery( )的末尾添加图像。如果您想修改日期以使其出现在开头或任何其他元数据中,请参阅此链接。 https://gist.github.com/samkirton/0242ba81d7ca00b475b9
TA贡献1963条经验 获得超6个赞
您可以通过 MediaStore 插入图像
MediaStore.Images.Media.InsertImage(this, yourBitmap, yourTitle, yourDescription);
注意:您可能想为此添加写入外部存储权限。
- 2 回答
- 0 关注
- 91 浏览
添加回答
举报