在布局中制作照片和显示(Xamarin Android)

时间:2017-03-20 14:26:51

标签: c# android xamarin xamarin.android

我编写制作照片的应用程序,保存到SD并显示到布局

我正在学习本教程

Link

但我也编写了将布局保存为位图的代码

这是代码

    using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.IO;
using Environment = Android.OS.Environment;
using Uri = Android.Net.Uri;
using Android.Provider;
using System.IO;
using Android.Graphics.Drawables;
using System;

namespace SaveViewAsBitMap
{

     public static class App
    {
        public static Java.IO.File _file;
        public static Java.IO.File _dir;
        public static Bitmap bitmap;
    }
    [Activity(Label = "SaveViewAsBitMap",  ScreenOrientation = ScreenOrientation.Landscape, Theme = "@android:style/Theme.Black.NoTitleBar")]
    public class Badge : Activity
    {
        public string name_from_activity;
        public string surname_from_activity;
        public string inn_from_activity;
        private ImageView _imageView;
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            // Make it available in the gallery

            Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
            Uri contentUri = Uri.FromFile(App._file);
            mediaScanIntent.SetData(contentUri);
            SendBroadcast(mediaScanIntent);

            // Display in ImageView. We will resize the bitmap to fit the display
            // Loading the full sized image will consume to much memory 
            // and cause the application to crash.

            int height = Resources.DisplayMetrics.HeightPixels;
            int width = _imageView.Height;
            App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);
            if (App.bitmap != null)
            {
                _imageView.SetImageBitmap(App.bitmap);
                App.bitmap = null;
            }

            // Dispose of the Java side bitmap.
            GC.Collect();
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            TextView bt1 = FindViewById<TextView>(Resource.Id.Surname);
            bt1.Click += Bt1_Click;
            name_from_activity = Intent.GetStringExtra("Name");
            surname_from_activity = Intent.GetStringExtra("Surname");
            inn_from_activity = Intent.GetStringExtra("INN");
            TextView Name = FindViewById<TextView>(Resource.Id.Name);
            Name.Text = name_from_activity;
            TextView Surname = FindViewById<TextView>(Resource.Id.Surname);
            Surname.Text = surname_from_activity;
            var barcodeWriter = new ZXing.Mobile.BarcodeWriter
            {
                Format = ZXing.BarcodeFormat.QR_CODE,
                Options = new ZXing.Common.EncodingOptions
                {
                    Width = 600,
                    Height = 600
                }
            };
            ImageView qr = FindViewById<ImageView>(Resource.Id.qr);
            var bitmap = barcodeWriter.Write(inn_from_activity);
            qr.SetImageBitmap(bitmap);
            if (IsThereAnAppToTakePictures())
            {
               _imageView = FindViewById<ImageView>(Resource.Id.photo);
                TakeAPicture();
            }
        }

        private void Bt1_Click(object sender, System.EventArgs e)
        {
            View v = FindViewById<LinearLayout>(Resource.Id.badge2);
            Bitmap myBitMap = createViewBitmap(v);
            Drawable drawable = new BitmapDrawable(myBitMap);
            //img.SetBackgroundDrawable(drawable);
           // MediaStore.Images.Media.InsertImage(ContentResolver, myBitMap, "title", "description");
            saveImage(myBitMap);
            Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
            Java.IO.File myFile = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/DCIM/Camera", "MikeBitMap4.jpg");
            Android.Net.Uri contentUri = Android.Net.Uri.FromFile(myFile);
            mediaScanIntent.SetData(contentUri);
            SendBroadcast(mediaScanIntent);
        }

        public Bitmap createViewBitmap(View v)
        {
            Bitmap bitmap = Bitmap.CreateBitmap(v.Width, v.Height,
                    Bitmap.Config.Argb8888);
            Canvas canvas = new Canvas(bitmap);
            v.Draw(canvas);
            return bitmap;
        }

          private void CreateDirectoryForPictures()
          {
              App._dir = new Java.IO.File(
                  Environment.GetExternalStoragePublicDirectory(
                      Environment.DirectoryPictures), "CameraAppDemo");
              if (!App._dir.Exists())
              {
                  App._dir.Mkdirs();
              }
          }

         private bool IsThereAnAppToTakePictures()
         {
             Intent intent = new Intent(MediaStore.ActionImageCapture);
             IList<ResolveInfo> availableActivities =
                 PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
             return availableActivities != null && availableActivities.Count > 0;
         }
        private void TakeAPicture()
        {
            Intent intent = new Intent(MediaStore.ActionImageCapture);

            App._file = new Java.IO.File(App._dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));

            intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(App._file));

            StartActivityForResult(intent, 0);
        }

        public static void saveImage(Bitmap bmp)
        {
             try
            {
                using (var os = new System.IO.FileStream(Android.OS.Environment.ExternalStorageDirectory + "/DCIM/Camera/MikeBitMap4.jpg", System.IO.FileMode.CreateNew))
                {
                    bmp.Compress(Bitmap.CompressFormat.Jpeg, 95, os);
                }
            }
            catch (Exception e)
            {

            }

        }

    }
}

我的问题是,我打开应用程序,打开相机,我拍照,但是当我点击接受时,什么都没有。为什么不回归布局?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

此方法出现错误

  private bool IsThereAnAppToTakePictures()
     {
         Intent intent = new Intent(MediaStore.ActionImageCapture);
         IList<ResolveInfo> availableActivities =
             PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
         return availableActivities != null && availableActivities.Count > 0;
     }

我忘了叫它