如何使用Camera xamarin Forms android捕获多张照片
我正在开发Xamrin表单应用程序。 在其中,我试图使用具有相同意图的相机拍摄多张照片。 没有使用任何nuget
包?
为此,我关注此链接相机
MainActivity.cs static readonly File file = new File(Environment.GetExternalStoragePublicDirectory( Environment.DirectoryPictures), "tmp.jpg");
在OnCreate结束时
(Xamarin.Forms.Application.Current as App).ShouldTakePicture += () => { var intent = new Intent(MediaStore.ActionImageCapture); intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(file)); StartActivityForResult(intent, 0); };
在这里我只点击了相机按钮上的1张照片,但我想拍摄更多照片,我想要它的文件路径
在OnActivityResult中
(Xamarin.Forms.Application.Current as App).ShowImage(file.Path);
但也许它几乎没有限制,例如我们无法一次拍摄多张照片? 有人知道如何捕获多张照片并将其绑定到listview吗?
是的,经过这么多的努力,我能够做到这一点。 对于以xamarinforms从事此类工作的人可能会有所帮助。
这是我的代码
public partial class App : Application { // public static App Instance; public App () { MainPage = new CameraGallery.MainPage(); InitializeComponent(); } }
MainPage.xaml中
//请安装FlowListView和ffimageloading nuget pckg
MainPage.xaml.cs中
public partial class MainPage : ContentPage { ObservableCollection camImageCollection; public static MainPage Instance; public MainPage() { InitializeComponent(); Instance = this; var btn = new Button { Text = "Snap!", Command = new Command(o => ShouldTakePicture()), }; CameraLayout.Children.Add(btn); camImageCollection = new ObservableCollection (); } public event Action ShouldTakePicture = () => { }; public void ShowImage(string[] filepath) { foreach(var item in filepath) camImageCollection.Add(item); listItemsCam.FlowItemsSource = camImageCollection; } }
现在转到你的MainActivity.cs里面的android项目
[Activity(Label = "CameraGallery", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { public static int OPENCAMERACODE = 102; //inside OnCreate after LoadApplication(new App()); add these lines protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; UserDialogs.Init(this); base.OnCreate(bundle); global::Xamarin.Forms.Forms.Init(this, bundle); FlowListView.Init(); CachedImageRenderer.Init(false); LoadApplication(new App()); MainPage.Instance.ShouldTakePicture += () => { ICursor cursor = loadCursor(); image_count_before = cursor.Count; cursor.Close(); Intent intent = new Intent(MediaStore.IntentActionStillImageCamera); IList activities = PackageManager.QueryIntentActivities(intent, 0); if(activities.Count >0) StartActivityForResult(Intent.CreateChooser(intent, "Camera Capture"), OPENCAMERACODE); }; } public ICursor loadCursor() { string[] columns = new string[] { MediaStore.Images.ImageColumns.Data, MediaStore.Images.ImageColumns.Id }; string orderBy = MediaStore.Images.ImageColumns.DateAdded; return ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, columns, null, null, orderBy); } private void exitingCamera() { ICursor cursor = loadCursor(); string[] paths = getImagePaths(cursor, image_count_before); MainPage.Instance.ShowImage(paths);// this parameter pass to MainPage.xaml.cs cursor.Close(); } public string[] getImagePaths(ICursor cursor, int startPosition) { int size = cursor.Count - startPosition; if (size <= 0) return null; string[] paths = new string[size]; int dataColumnIndex = cursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data); for (int i = startPosition; i < cursor.Count; i++) { cursor.MoveToPosition(i); paths[i - startPosition] = cursor.GetString(dataColumnIndex); } return paths; } //inside OnActivityResult method do this protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); switch (requestCode) { case 102: exitingCamera(); break; } } }
我希望这有助于某人。 享受编码......
- 每个人工创建的代码文件的开头都有文件头注释吗?
- Combobox将itemsource绑定到自定义列表,将selecteditem绑定到该列表的实例不起作用
- 如何使用C#以编程方式获取正在运行的程序的产品(而非汇编)版本?
- 从IntPtr获取struct数组
- 为什么.NET没有像Soft一样的SoftReference和WeakReference?
- C#Windows 8 Store(Metro,WinRT)Byte数组到BitmapImage
- 使用单独的列在c#中创建分隔文本
- 带有自定义身份validation的ASP.NET Web API
- 为什么LINQ to SQL不支持查询运算符’ElementAt’?