How to save images to gallery in xamarin forms using plugins - image

I want to save images to gallery which is in the drawable folder. I built an app to take pictures and save it in the gallery in xamarin forms by using the following article.
https://xamarinhelp.com/use-camera-take-photo-xamarin-forms/
private async void SaveButton_Clicked(object sender, EventArgs e){
var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
{
PhotoSize = PhotoSize.Medium,
CompressionQuality = 92,
SaveToAlbum = true, //saves to image gallery
Directory = "MyFolder2",
Name = $"{DateTime.Now}_documenttypeXYZ.jpg"
});
}
the above article is using media plugin https://github.com/jamesmontemagno/MediaPlugin/blob/master/README.md#saving-photovideo-to-camera-rollgallery to take pictures and save it.
But i don't know how to save the images to gallery which are in the drawable folder.

Related

Image From Music Library Not Showing in UWP app

I'm new to UWP development & I'm just showing image in my app from the Music Library.
Infact, I have added Music Library in the app's "Capabilities" & I can confirm that I have access to Music Library as I can read & write files in it.
But when I try to load a image in XAML, it just does not shows...
<Image Height="200" Width="200" Source="C:/Users/Alex Mercer/Music/Album/albumArt.png" />
Please help me understand & solve the problem.
😃 Thanks a lot!
Although we enable the corresponding capabilities, accessing files through paths is still strictly restricted in UWP.
In fact, it is not a good idea to write the full path in XAML, because you cannot guarantee that the path must exist on the device where the application is installed.
To display the pictures in the music library, you can do this:
xaml
<Image Height="200" Width="200" x:Name="AlbumImage" Loaded="AlbumImage_Loaded"/>
xaml.cs
private async void AlbumImage_Loaded(object sender, RoutedEventArgs e)
{
try
{
var albumFolder = await KnownFolders.MusicLibrary.GetFolderAsync("Album");
var albumPic = await albumFolder.GetFileAsync("albumArt.png");
var bitmap = new BitmapImage();
using (var stream = await albumPic.OpenAsync(FileAccessMode.Read))
{
await bitmap.SetSourceAsync(stream);
}
AlbumImage.Source = bitmap;
}
catch (FileNotFoundException)
{
// File or Folder not found
}
catch (Exception)
{
throw;
}
}

JavaFX show photos in ImageView not working

I'm trying to display a photo depending on the BMI of the user.. So for example if your BMI is under 18.5 it will show a thumbs down.
All the images are located in my resource folder. But the image doesn't show up in the program as it should.. I've tried serveral images to see if it had something to do with the resolution. This is my code:
#FXML
private void tellUserStatus(){
date = new Date();
if (bmi<18.5){
System.out.println(dateFormat.format(date)+"User is underweight - BMI is under 18.5");
BmiStatusMessage.setVisible(true);
BmiStatusMessage.setText("You are underweight");
File underweight= new File("underweight-thumbsdown.png");
Image image = new Image(underweight.toURI().toString());
BmiConclusion.setVisible(true);
BmiConclusion.setImage(image);
}
}
BmiStatusMessage is a Label, and BmiConclusion is an ImageView. They are both linked with the fxml file.

Image in Tizen Mobile is not visible

I am using <Image Source="flower.png" /> code in Xamarin form in Visual Studio 2017 to show image in Tizen Mobile but it does not show up. I put image file in shared\res folder in Tizen project.
Copy the Image into '.TizenMobile' Project 'res' directory (not in 'shared>res' only 'res' (root)). Then In Solution Explorer:Right click over 'res' folder and Add > Existing Item > png Image
In your ProjectCode (Portable) C# file:
MainPage = new ContentPage
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Image {
Aspect = Aspect.AspectFit,
Source = ImageSource.FromFile("flower.png")
}
}
}
};
Or you can even try the XAML.

How to show image files from a directory in a flipview?

I found many samples of displaying images from a resource in a Windows Store app and got it to display images within a sample, but I would require the flipview to show images in a directory, or at least to show image file names I provide by code. With everything I tried so far the flipview remains empty. I maybe missing something obvious, this is the relevant part of the XAML:
<FlipView x:Name="flipView1" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="809,350,9,7" Width="548" Height="411" >
<FlipView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path=Image }" Stretch="Uniform"/>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
this works, but it requires me to add the images a resource first....
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/P1000171.jpg"));
FlipViewItem flipvw1 = new FlipViewItem();
flipvw1.Background = brush1;
flipView1.Items.Add(flipvw1);
but (for example) this doesn't:
string name = String.Format(#"c:\temp\P1000171.JPG");
Uri uri = new Uri(name);
BitmapImage img = new BitmapImage(uri);
flipView1.Items.Add(img);
What do I miss?
In the meantime I've found the answer myself which I now add for future readers. The above example won't work because a Windows 8 app isn't allowed to access most of the PC's directories without the user having selected one using a FolderPicker. The program can re-use that directory later with:
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
I've changed the above XAML here:
<Image Source="{Binding}" Stretch="UniformToFill"/>
The Task below will show all .JPG files in the Pictures library in a Flipview, if, in the Package.appxmanifest, Capabilities, "Pictures library" has been checked:
public async Task flipviewload()
{
IReadOnlyList<StorageFile> fileList = await picturesFolder.GetFilesAsync();
var images = new List<BitmapImage>();
if (fileList != null)
{
foreach (StorageFile file in fileList)
{
string cExt = file.FileType;
if (cExt.ToUpper()==".JPG")
{
Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
using (Windows.Storage.Streams.IRandomAccessStream filestream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
images.Add(bitmapImage);
}
}
}
}
flpView.ItemsSource = images;
}

Saving an image from image control to gallery in windows phone 7

I have a image in a Image control like below:
<Image x:name="myImg" Source="Images/MyImg.png" />
How can I save this image in to the image gallery so that I can see by going to the gallery folder.
I have tried different codes but I am not able to save it. Please help me on this.
EDIT:
I have image in control in List Box. I am binding the list box with the IList that is coming from the web service.
So after Binding the image if user want to save the image he can save the particular which he want to save.
So how can i save the that particular image.
Thanks in advance.
There is an excellent MSDN article that describes this very scenario:
How to: Encode a JPEG for Windows Phone and Save to the Pictures Library
Incidentally , it's also the first search result link when searching for windows phone save image to media library on both Google and Bing.
Have you tried following this guide, and if so what are you having problems with?
The essential code to save it:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
// Create a file name for the JPEG file in isolated storage.
String tempJPEG = "TempJPEG";
// Create a virtual store and file stream. Check for duplicate tempJPEG files.
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(tempJPEG))
{
myStore.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);
// Create a stream out of the sample JPEG file.
// For [Application Name] in the URI, use the project name that you entered
// in the previous steps. Also, TestImage.jpg is an example;
// you must enter your JPEG file name if it is different.
StreamResourceInfo sri = null;
Uri uri = new Uri("[Application Name];component/TestImage.jpg", UriKind.Relative);
sri = Application.GetResourceStream(uri);
// Create a new WriteableBitmap object and set it to the JPEG stream.
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode the WriteableBitmap object to a JPEG stream.
wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
myFileStream.Close();
// Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);
// Save the image to the camera roll or saved pictures album.
MediaLibrary library = new MediaLibrary();
if (radioButtonCameraRoll.IsChecked == true)
{
// Save the image to the camera roll album.
Picture pic = library.SavePictureToCameraRoll("SavedPicture.jpg", myFileStream);
MessageBox.Show("Image saved to camera roll album");
}
else
{
// Save the image to the saved pictures album.
Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
MessageBox.Show("Image saved to saved pictures album");
}
myFileStream.Close();
}

Resources