简体   繁体   English

在Xamarin表单中选择或选择多个图像

[英]Take or Select Multiple Image in Xamarin Forms

I am using "Plugin.Media.CrossPlatform" nuget for accessing Camera and take pictures in Xamarin Forms. 我使用“Plugin.Media.CrossPlatform”nuget来访问相机并用Xamarin Forms拍照。 But currently, it takes only a single picture at a time and upload only that last taken picture. 但目前,它一次只需要一张图片,只上传最后拍摄的图片。 I want to add multiple captures and allow a user to select a specific picture from recently captured images. 我想添加多个捕获并允许用户从最近捕获的图像中选择特定图片。 How's that possible with this plugin in Android and IOS both Platform? 这个插件在Android和IOS这两个平台上怎么可能?

Also, I have added selection mode, If a user wants to select a picture from gallery. 此外,我添加了选择模式,如果用户想要从图库中选择图片。 But there is the same issue. 但是存在同样的问题。 I want to select multiple images at a time and upload it, on both platforms. 我想一次选择多个图像并在两个平台上上传。 How's this implement in Xamarin Forms? Xamarin Forms中的这个工具怎么样? Is there any example or blog for this task? 这个任务有没有例子或博客?

I am using this package. 我正在使用这个包。

The MediaPlugin doesn't allow multiple captures or picking up multiple pictures. MediaPlugin不允许多次拍摄或拍摄多张照片。

You have to implement platform methods and access them with Dependency Service, something like that. 您必须实现平台方法并使用Dependency Service访问它们,就像这样。 DependencyService.Get<IDoMultipleStuff>().TakePictures();

For Android you could take multiple pictures using INTENT_ACTION_STILL_IMAGE_CAMERA : 对于Android,您可以使用INTENT_ACTION_STILL_IMAGE_CAMERA拍摄多张照片:

Intent intent = new Intent(
    MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
this.startActivity(intent);

While for iOS you can use PhotoPicker that can allow you to take multiple pictures before going back to your app. 对于iOS,您可以使用PhotoPicker ,它可以让您在返回应用程序之前拍摄多张照片。

In Android, you can select multiple images by putting in the Intent the extra ExtraAllowMultiple 在Android中,您可以通过在Intent中添加额外的ExtraAllowMultiple来选择多个图像

var imageIntent = new Intent(Intent.ActionPick);
imageIntent.SetType ("image/*");
imageIntent.PutExtra (Intent.ExtraAllowMultiple, true);
imageIntent.SetAction (Intent.ActionGetContent);
((Activity)Forms.Context).StartActivityForResult(Intent.CreateChooser (imageIntent, "Select photo"), 0);

For iOS, you can use ELCImagePicker in this way. 对于iOS,您可以通过这种方式使用ELCImagePicker

var picker = ELCImagePickerViewController.Create();
picker.MaximumImagesCount = 15;

picker.Completion.ContinueWith (t => {
  if (t.IsCanceled || t.Exception != null) {
    // no pictures for you!
  } else {
     var items = t.Result as List<AssetResult>;
   }
});

PresentViewController (picker, true, null);

Xamarin Android forms take multiple pictures from Camera this is the solution for it. Xamarin Android表单从Camera获取多张图片,这是它的解决方案。

public partial class App : Application
{
   // public static App Instance;

    public App ()
    {
        MainPage = new CameraGallery.MainPage();
        InitializeComponent();

    }
}

MainPage.xaml MainPage.xaml中

// please install FlowListView and ffimageloading nuget pckg //请安装FlowListView和ffimageloading nuget pckg

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CameraGallery"
             x:Class="CameraGallery.MainPage"
             xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
             xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"> 

            <StackLayout x:Name="CameraLayout">
                <flv:FlowListView FlowColumnCount="3" x:Name="listItemsCam" 
                        SeparatorVisibility="None"
                        HasUnevenRows="false" RowHeight="100" >
                    <flv:FlowListView.FlowColumnTemplate>
                        <DataTemplate >
                            <ffimageloading:CachedImage  DownsampleToViewSize="true" AbsoluteLayout.LayoutFlags="All" HeightRequest="100" AbsoluteLayout.LayoutBounds="0,0,1,1" Source="{Binding .}"  Aspect="AspectFill" HorizontalOptions="FillAndExpand">
                            </ffimageloading:CachedImage>
                        </DataTemplate>
                    </flv:FlowListView.FlowColumnTemplate>
                </flv:FlowListView>
                <!--<Image x:Name="image" IsVisible="False"></Image>-->
            </StackLayout>
</ContentPage>  

MainPage.xaml.cs MainPage.xaml.cs中

public partial class MainPage : ContentPage
    {
        ObservableCollection<string> 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<string>();
        }
        public event Action ShouldTakePicture = () => { };

        public void ShowImage(string[] filepath)
        {
           foreach(var item in filepath)
             camImageCollection.Add(item);
             listItemsCam.FlowItemsSource = camImageCollection;
        }
    }

now go to your android project inside it MainActivity.cs 现在转到你的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<ResolveInfo> 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;
            }
        }
    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM