Hi, Xamarin forum the following is my code for selecting multiple images now I dont know how can I grab those images and save it to database I apologize if my code is not properly formatted coz im using mobile phone
XAML
CodeBehind
public partial class PostCreationPage : ContentPage
{
public PostCreationPage()
{
InitializeComponent();
}
private MediaFile _mediaFile;
private string URL { get; set; }
ImageSource ImageSrc;
List _images = new List();
private async void Select_Pic(object sender, EventArgs e)
{
string[] fileTypes = null;
if (Device.RuntimePlatform == Device.Android)
{
fileTypes = new string[] { "image/jpeg" };
}
if (Device.RuntimePlatform == Device.iOS)
{
fileTypes = new string[] { "public.image" };
}
await DependencyService.Get().OpenGallery();
}
private async void Upload_File(object sender, EventArgs e)
{
if (_images == null)
{
await DisplayAlert("Error", "There was an error occured when trying to get your image.", "OK");
return;
}
else
{
UploadImage(_mediaFile.GetStream());
}
}
private async void UploadImage(Stream stream)
{
string userfirstname = App.FirstName;
Pipeline databaseConnect = new Pipeline();
var page = new LoadingPage();
//await stream.ReadAsync(bytes, 0, (int)stream.Length);
try
{
await PopupNavigation.Instance.PushAsync(page);
SqlCommand sqlInsert = new SqlCommand("INSERT INTO t_Post(Post_Content, Photo, Photo_2, Photo_3, Photo_4, Uploader, Date_Uploaded) " +
"VALUES(@PostContent, @Photo, @Photo2, @Photo3, @Photo4, @Uploader, @DateUploaded)", databaseConnect.connectDB());
sqlInsert.Parameters.AddWithValue("@PostContent", postContent.Text);
sqlInsert.Parameters.AddWithValue("@Photo", SqlDbType.Image);
sqlInsert.Parameters.AddWithValue("@Photo2", SqlDbType.Image);
sqlInsert.Parameters.AddWithValue("@Photo3", SqlDbType.Image);
sqlInsert.Parameters.AddWithValue("@Photo4", SqlDbType.Image);
sqlInsert.Parameters.AddWithValue("@Uploader", userfirstname);
sqlInsert.Parameters.AddWithValue("@DateUploaded", DateTime.Now);
int i = sqlInsert.ExecuteNonQuery();
databaseConnect.connectDB().Close();
if (_images == null)
{
await DisplayAlert("Error", "There was an error when trying to get your image.", "OK");
return;
}
else
{
await DisplayAlert("Success", "Uploaded to Database.", "OK");
await PopupNavigation.Instance.PopAsync();
await Navigation.PopAsync();
}
}
catch (Exception ex)
{
}
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe((App)Xamarin.Forms.Application.Current, "ImagesSelected", (s, images) =>
{
listItems.FlowItemsSource = images;
_images = images;
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe(this, "ImagesSelected");
}
}
IMediaService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ProjectX.Services
{
public interface IMediaService
{
Task OpenGallery();
void ClearFiles(List filePaths);
}
}
XAML
CodeBehind
public partial class PostCreationPage : ContentPage
{
public PostCreationPage()
{
InitializeComponent();
}
private MediaFile _mediaFile;
private string URL { get; set; }
ImageSource ImageSrc;
List _images = new List();
private async void Select_Pic(object sender, EventArgs e)
{
string[] fileTypes = null;
if (Device.RuntimePlatform == Device.Android)
{
fileTypes = new string[] { "image/jpeg" };
}
if (Device.RuntimePlatform == Device.iOS)
{
fileTypes = new string[] { "public.image" };
}
await DependencyService.Get().OpenGallery();
}
private async void Upload_File(object sender, EventArgs e)
{
if (_images == null)
{
await DisplayAlert("Error", "There was an error occured when trying to get your image.", "OK");
return;
}
else
{
UploadImage(_mediaFile.GetStream());
}
}
private async void UploadImage(Stream stream)
{
string userfirstname = App.FirstName;
Pipeline databaseConnect = new Pipeline();
var page = new LoadingPage();
//await stream.ReadAsync(bytes, 0, (int)stream.Length);
try
{
await PopupNavigation.Instance.PushAsync(page);
SqlCommand sqlInsert = new SqlCommand("INSERT INTO t_Post(Post_Content, Photo, Photo_2, Photo_3, Photo_4, Uploader, Date_Uploaded) " +
"VALUES(@PostContent, @Photo, @Photo2, @Photo3, @Photo4, @Uploader, @DateUploaded)", databaseConnect.connectDB());
sqlInsert.Parameters.AddWithValue("@PostContent", postContent.Text);
sqlInsert.Parameters.AddWithValue("@Photo", SqlDbType.Image);
sqlInsert.Parameters.AddWithValue("@Photo2", SqlDbType.Image);
sqlInsert.Parameters.AddWithValue("@Photo3", SqlDbType.Image);
sqlInsert.Parameters.AddWithValue("@Photo4", SqlDbType.Image);
sqlInsert.Parameters.AddWithValue("@Uploader", userfirstname);
sqlInsert.Parameters.AddWithValue("@DateUploaded", DateTime.Now);
int i = sqlInsert.ExecuteNonQuery();
databaseConnect.connectDB().Close();
if (_images == null)
{
await DisplayAlert("Error", "There was an error when trying to get your image.", "OK");
return;
}
else
{
await DisplayAlert("Success", "Uploaded to Database.", "OK");
await PopupNavigation.Instance.PopAsync();
await Navigation.PopAsync();
}
}
catch (Exception ex)
{
}
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe((App)Xamarin.Forms.Application.Current, "ImagesSelected", (s, images) =>
{
listItems.FlowItemsSource = images;
_images = images;
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe(this, "ImagesSelected");
}
}
IMediaService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ProjectX.Services
{
public interface IMediaService
{
Task OpenGallery();
void ClearFiles(List filePaths);
}
}