Saving images and retrieving them Xamarin iOS - c#

So after recently updating dependencies including Xamarin Forms, I can no longer see saved images within our app in iOS. We use https://github.com/jamesmontemagno/MediaPlugin for adding/taking pictures and have even tried specific OS file writes to environment folder:
iOS specific for saving an image:
public string WriteFile(byte[] data)
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var filePath = Path.Combine(path, CameraHelper.GenerateFileName());
File.WriteAllBytes(filePath, data);
return filePath;
}
The images are saved under an ID that changes after every app update. This obviously breaks our previous method as we saved the image path (which was always the same prior) in the tables and used that to show preview of the images.
Here is the new image path:
"/var/mobile/Containers/Data/Application/215E777E-A624-487E-B687-6647F8C0D1DC/Documents/01_18_2021_04_24_07_98524.jpg"
Old path:
/data/user/0/com.company.ourapp/files/Pictures/
Which essentially makes it impossible for us to see pictures taken from a previous build/version of the app. I have scoured Xamarin Forms/Essentials and Stack Overflow but no luck.
Is there no other way to retrieve the images? Or do we have to set the library paths to be dynamic for our views?

Old path:
/data/user/0/com.company.ourapp/files/Pictures/
It is obviously an Android file path instead of a iOS file Path. Android's file system is different from iOS.
Here is the new image path:
"/var/mobile/Containers/Data/Application/215E777E-A624-487E-B687-6647F8C0D1DC/Documents/01_18_2021_04_24_07_98524.jpg"
This is the right path to iOS document folder in sandbox. See this document about the Application directories in iOS.
I think you should distinguish iOS and Android when retrieving the images.
Which essentially makes it impossible for us to see pictures taken
from a previous build/version of the app.
If the path really changes with different versions, you can get the app version number at runtime and give different filePath to different version.
(While I think your problem is the difference between iOS and Android file path.)

Related

How to get the path name of an image inside the properties.resources in Microsoft .NET Framework?

this is my first question in stack overflow (Ye hey)!
So I am using Itext 7 to create PDF files in my program. When creating a PDF, the document inside has an image logo. It work when I run the program in debug mode using this path name:
string imageFile = "../../Resources/smIcon(Transparent).png";
ImageData imagedata = ImageDataFactory.Create(imageFile);
Image image = new Image(imagedata).SetHeight(100).SetWidth(200);
document.Add(image);
However, publishing the project and running the application does not let me create the PDF due to the Path name... I tried publishing without the logo by removing the code above and it works to create a PDF document, which I suspect the issue is the path name.
Would be great to know if you guys have a solution for this! Thank you.
Found a way to work on it with a help of a good friend.
ImageData imagedata = ImageDataFactory.Create(Properties.Resources.smIcon_Transparent_, null);
Image image = new Image(imagedata).SetHeight(100).SetWidth(200);
So basically with this way, I don't need to get the actual path name of the file but instead get the one that is in the properties.resource, where publishing the project does not need to change the path.

C#, How to Traverse all drive and folder to copy image and paste them in a specific folder?

my project was a image gallery using asp.net.
Initially when then the project load first time i need to copy all the images in the computer and paste them in a folder named Data.
here is my project link:
https://www.dropbox.com/s/sr3qhaamiiwk0di/Gallary_temp.zip?dl=0
https://drive.google.com/file/d/0BynGaI0gi3mrOEtweWlWOEdmSFE/view?usp=sharing
Of course, code must be in c#
Instead of copying the image from hard drive to your location every time, it is better you save the file location in DB and call them when ever you require.

Using files in my Windows Phone app, resource, content..?

I just started on my second Windows Phone application, and I ran into a problem (ehm..challange). In my app, I have added a folder called "Images", and some sub-folders, like this:
Images -> boys -> graphs
The boys folder contains 1 xml file, and the graphs folder contains hundreds of .gif images. And I'm now wondering how to access these files, how can I build them into the .xap file, what's the recommended way to go?
Should I set the files to "Content" or "Resource", "Copy to output folder"?
And how do I refer to these files, both the xml file in "boys", and the gif images in "graphs"?
A bit confused, and hoping that there is someone who can kick me in the right direction :)
edit, more info
I got it working, loading the xml file with the code below, I haven't tried with the images yet, but is this "the way to do it"?
string xmlBoys = #"images/boys/Names.xml";
Uri uri = new Uri(xmlBoys, UriKind.Relative);
StreamResourceInfo sm = Application.GetResourceStream(uri);
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sm.Stream);
XDocument data = XDocument.Load(xr);
If you set the BuildAction on the images to Content you can refer to them by the relative path.
e.g.: new Uri("\Images\boys\graphs\img1.gif", UriKind.Relative);
You don't need to "Copy to output folder" unless you need them there for your own purposes. You probably don't. That option is an artefact of other types of project. Everythign you need to distribute will be bundled in the XAP.
Importantly, however, note that WP7 does not have native support for gif format files. I'd suggest converting these to PNG before trying to use them.

C# wpf html help file

C3, WPF, Windows 7, Microsoft Ribbon, Visual studio 2010, dotnet 4.5
I would like to write the help files in html and include them inside the .exe file as an resource.
Is there a way of telling the explorer to get the html pages directly from the .exe file ?
If not, I assume the only way to archive it is to create a temporary directory, copy the
files and start the browser pointing to these files.
Is there a magic call that takes a resource directory and unpack/copy it to a directory ?
Like
tempDir = GetTempDirectory();
WPF_MAGIC_RESOURCE_UNPACKER("/help/*", tempDir );
System.Diagnostics.Process.Start(tempDir + Path.DirectorySeparatorChar + "index.html");
Regards Stefan
I agree with #Tigran, these should be stored separate to the binary or even online, but if you feel as though this is the best solution you could probably make use of the Pack URI functionality of WPF.
This is primarily used for images such as icon resources and splash screens, but could be used for any type of content.
You should be able to extract these resources and save them to the file system or to a stream and use as you see fit.
Example: Uri absoluteUri = new Uri("pack://application:,,,/help/contents.htm", UriKind.Absolute);

Images from common project

I have 4 projects in same solution. In one project the images get uploaded & stored in some folder.Now I want to show this image in another project which is in same solution.What code should I write in C#?
It seems to me that it would be sensible that the same project responsible for storing the images should be responsible for retrieving them too... either by providing a filename "mapping" service (e.g. original upload filename mapped to physical location on disc) or by giving a method which will open the file and return a Stream to the data. Then showing the image becomes a matter of calling that method and then loading the image as normal.
It's hard to be more precise without more details of what you're trying to do, how the image is stored etc. What have you already tried, and what problems have you run into?
You can keep the ImageFolder path in config file in all the project.
And that path can be use to Read/Write the image(s).

Categories

Resources