如何使用我制作的应用程序将图像保存到图库?

我有这个Xamarin Android 应用程序(不是 Forms),它可以打开相机并让我拍照并随拍或拍一张新照片。之后,ImageView使用位图向我展示应用程序上的图片。我无法使用位图保存到画廊(我不知道该怎么做,或者是否有更简单的方法)。我需要应用程序获取应用程序拍摄的最后一张照片(为什么需要保存它)并通过单击按钮将其发送到服务器(我也需要一些帮助)。这就是我需要做的。这是代码MainActivity.cs:


using Android.App;


using Android.Widget;


using Android.OS;


using Android.Content;


using Android.Provider;


using Android.Runtime;


using Android.Graphics;


namespace CameraApp


{


    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]


    public class MainActivity : Activity


    {


        ImageView imageView;


        protected override void OnCreate(Bundle bundle)


        {


            base.OnCreate(bundle);


            SetContentView(Resource.Layout.activity_main);


            Button btnCamera = FindViewById<Button>(Resource.Id.btnCamera);


            imageView = FindViewById<ImageView>(Resource.Id.imageView);


            btnCamera.Click += BtnCamera_Click;


        }


        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)


        {


            base.OnActivityResult(requestCode, resultCode, data);


            Bitmap bitmap = (Bitmap)data.Extras.Get("data");


            imageView.SetImageBitmap(bitmap);


        }


        private void BtnCamera_Click(object sender, System.EventArgs e)


        {


            Intent intent = new Intent(MediaStore.ActionImageCapture);


            StartActivityForResult(intent, 0);


        }


    }


}


人到中年有点甜
浏览 101回答 2
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{&nbsp; &nbsp; Button button1;&nbsp; &nbsp; ImageView imageView1;&nbsp; &nbsp; View layout;&nbsp; &nbsp; static readonly int REQUEST_CAMERA_WriteExternalStorage = 0;&nbsp; &nbsp; protected override void OnCreate(Bundle savedInstanceState)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; // Set our view from the "main" layout resource&nbsp; &nbsp; &nbsp; &nbsp; SetContentView(Resource.Layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; layout = FindViewById<RelativeLayout>(Resource.Id.sample_main_layout);&nbsp; &nbsp; &nbsp; &nbsp; button1 = FindViewById<Button>(Resource.Id.button1);&nbsp; &nbsp; &nbsp; &nbsp; imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);&nbsp; &nbsp; &nbsp; &nbsp; button1.Click += (o, e) =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckPermission();&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; &nbsp; public&nbsp; void CheckPermission()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ((ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == (int)Permission.Granted)&& (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == (int)Permission.Granted))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Camera and store permission has&nbsp; been granted&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowCamera();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Camera and store permission has not been granted&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RequestPermission();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void RequestPermission()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.Camera, Manifest.Permission.WriteExternalStorage }, REQUEST_CAMERA_WriteExternalStorage);&nbsp; &nbsp; }&nbsp; &nbsp; //get result of persmissions&nbsp; &nbsp; public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (requestCode == REQUEST_CAMERA_WriteExternalStorage)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( PermissionUtil.VerifyPermissions(grantResults))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // All required permissions have been granted, display Camera.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowCamera();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // permissions did not grant, push up a snackbar to notificate USERS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Snackbar.Make(layout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.OnRequestPermissionsResult(requestCode, permissions, grantResults);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void ShowCamera()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(MediaStore.ActionImageCapture);&nbsp; &nbsp; &nbsp; &nbsp; StartActivityForResult(intent, 0);&nbsp; &nbsp; }&nbsp; &nbsp; protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; &nbsp; &nbsp; Bitmap bitmap=null;&nbsp; &nbsp; &nbsp; &nbsp; //If user did not take a photeo , he will get result of bitmap, it is null&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bitmap = (Bitmap)data.Extras.Get("data");&nbsp; &nbsp; &nbsp; &nbsp; } catch(Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.Error("TakePhotoDemo1", e.Message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.MakeText(this, "You did not take a photo", ToastLength.Short).Show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (bitmap != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MediaStore.Images.Media.InsertImage(ContentResolver, bitmap, "screen", "shot");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView1.SetImageBitmap(bitmap);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.MakeText(this, "You did not take a photo", ToastLength.Short).Show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}PermissionUtil.cs通过验证给定数组中的每个条目是否具有值 Permission.Granted,检查是否已授予所有给定权限。&nbsp; &nbsp;public abstract class PermissionUtil{&nbsp; &nbsp; public static bool VerifyPermissions(Permission[] grantResults)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // At least one result must be checked.&nbsp; &nbsp; &nbsp; &nbsp; if (grantResults.Length < 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; // Verify that each required permission has been granted, otherwise return false.&nbsp; &nbsp; &nbsp; &nbsp; foreach (Permission result in grantResults)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result != Permission.Granted)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}有我的代码。https://github.com/851265601/TakePhotoDemo1MediaStore.Images.Media.InsertImage前面的代码将在 gallery( )的末尾添加图像。如果您想修改日期以使其出现在开头或任何其他元数据中,请参阅此链接。 https://gist.github.com/samkirton/0242ba81d7ca00b475b9

神不在的星期二

您可以通过 MediaStore 插入图像MediaStore.Images.Media.InsertImage(this,&nbsp;yourBitmap,&nbsp;yourTitle,&nbsp;yourDescription);注意:您可能想为此添加写入外部存储权限。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go