[英]How to Capture multiple photos with Camera xamarin Forms android
I am developing Xamrin form application. 我正在开发Xamrin表单应用程序。 Inside it, I am trying to capture multiple photos using a camera with the same intent. 在其中,我试图使用具有相同意图的相机捕获多张照片。 without using any nuget
package? 不使用任何nuget
包?
for this, I am following this link Camera 为此,我正在关注此链接相机
MainActivity.cs
static readonly File file = new File(Environment.GetExternalStoragePublicDirectory(
Environment.DirectoryPictures), "tmp.jpg");
At the end of OnCreate 在OnCreate的结尾
(Xamarin.Forms.Application.Current as App).ShouldTakePicture += () => {
var intent = new Intent(MediaStore.ActionImageCapture);
intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(file));
StartActivityForResult(intent, 0);
};
here I am getting only 1 photo on clicking camera button but I want to capture more photo and I want its file path 在这里,单击“相机”按钮时我只会得到1张照片,但我想捕获更多照片,并且想要其文件路径
in OnActivityResult 在OnActivityResult中
(Xamarin.Forms.Application.Current as App).ShowImage(file.Path);
but maybe it has few limitations such as we can not capture multiple photos at a time? 但也许没有什么限制,例如我们一次不能拍摄多张照片? Is anyone is having an idea how to capture multiple photos and bind it to listview? 有谁知道如何捕获多张照片并将其绑定到Listview?
Yes after so much struggle I am able to do this. 是的,经过这么多的努力,我能够做到这一点。 It may be helpful for someone who is working on such things in xamarin forms. 这可能会对从事xamarin形式的此类工作的人有所帮助。
This is my code 这是我的代码
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;
}
}
}
I hope this help someone. 我希望这可以帮助某人。 enjoy coding... 享受编码...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.