2 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
我寫(xiě)了一個(gè)關(guān)于你的需求的演示。我添加了相機(jī)和外部存儲(chǔ)權(quán)限并將圖像存儲(chǔ)到畫(huà)廊,這里正在運(yùn)行 GIF。
https://i.stack.imgur.com/FPLUY.gif
有運(yùn)行demo的代碼。我添加了運(yùn)行時(shí)權(quán)限(相機(jī)和WriteExternalStorage)請(qǐng)求。并判斷用戶沒(méi)有拍照的情況,然后返回應(yīng)用程序。
[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通過(guò)驗(yàn)證給定數(shù)組中的每個(gè)條目是否具有值 Permission.Granted,檢查是否已授予所有給定權(quán)限。
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( )的末尾添加圖像。如果您想修改日期以使其出現(xiàn)在開(kāi)頭或任何其他元數(shù)據(jù)中,請(qǐng)參閱此鏈接。 https://gist.github.com/samkirton/0242ba81d7ca00b475b9

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以通過(guò) MediaStore 插入圖像
MediaStore.Images.Media.InsertImage(this, yourBitmap, yourTitle, yourDescription);
注意:您可能想為此添加寫(xiě)入外部存儲(chǔ)權(quán)限。
- 2 回答
- 0 關(guān)注
- 122 瀏覽
添加回答
舉報(bào)