access assets from monodroid class library - xamarin.android

Is it possible to access files (with build action "AndroidAsset") from a monodroid class library in a monodroid application that references the class library ?
I have created an "Assets" folder in the class lib and added a text file with build action "AndroidAsset", but from the app I could not access it via Assets.Open("file.txt");
I was hoping that the Assets from the class lib and the main application could somehow be "merged"...

No - you a can't access the assets from a class lib - only from the main exe project.
This is similar to the situation with Java Android projects.
One route around this might be to use embedded resources instead - see the answer to this question I asked on xamarin's forums - http://forums.xamarin.com/discussion/186/is-there-a-cross-platform-way-to-include-string-resources-from-class-library-projects - note that I haven't tried this yet

Is it possible to access files (with build action "AndroidAsset") from a monodroid class library in a monodroid application that references the class library ?
Yes. Just access them "as normal" via Context.Assets.Open. There is only one source of assets in the application, so any component can access any and all assets, if they have access to an AssetManager instance.
However, that just takes what you're asking at face-value. Can a Library project use the Context.Assets property? Certainly, if you provide it a Context instance.
But can a Library project provide it's own assets for inclusion into the application? No.
I have created an "Assets" folder in the class lib and added a text file with build action "AndroidAsset", but from the app I could not access it via Assets.Open("file.txt");
Library projects cannot provide assets that will be included in the larger application. Java doesn't support this either, afaik.

Things to remember
1 Build Action is AndroidAsset (Properties)
2 Copy to output directory Do not copy (Properties)
3 Add the file to the Assets folder
var destinationPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "yourfilename.txt");
using (Stream source = Assets.Open("yourfilename.txt"))
using (var dest = System.IO.File.Create (destinationPath)) {
source.CopyTo (dest);
}
Sometimes this will still fail, monodroid bug. The best thing to do is check to see if the files were included correctly.
Load up Eclipse, in the device browser, you should see your simulator.
Go to the directory data/app/ download the file yourappname.apk
This is just a zip file, so change the extension to .zip and open the
zip archive. And goto the folder /assets and see if the file you are trying
to load is in there.
Also if you try to grab a file off a physical device your testing on, some allow it and other don't. This is easiest to test on the simulator.

It is possible to access the Assets files from a library:
var sourcePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
//var files = Directory.GetFiles(sourcePath);
var filePath = Path.Combine(sourcePath, "MyAssetFile.bin");
var content = File.ReadAllBytes(filePath);

Related

How to view files(docx, ppt, xlsx, etc.) from my project’s assets in flutter?

Im working on a flutter app that needs to view document files. Is there a way to open or view document files from my assets?
FlutterPdfViewer.loadAsset(filepath) works fine but OpenFile.open(filepath) only works with files within the phone’s storage.
I can’t find any packages to view other file types besides pdf files from my project’s assets. Are there other ways? I hope you can help me.
I had the same problem and didn't find a solution, so I think, that open_file plugin calls a native app to open a file. That's why you can't open a file from assets using other application.
I think that you have to save that file in storage, after that call open_file OpenFile.open() method.

How do I put content into the iOS Documents folder at compile time, using Xamarin?

I have a data file that I need to include with my app when I distribute it. When loading any files in the app, I prefix the file name with:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
This works great for anything I create within the app (and for reading back), like files I download in response to a user action. But I can't for the life of me figure out how to place files there when I build my app in Visual Studio.
I've tried making a "Documents" subdirectory in the special "Resources" folder, but that didn't work (I tried setting the "Build Action" to both BundleResource and Content). When I look at the folder for my app (from using the simulator) I can see that in the "Documents" folder there's all the files I downloaded, but I can't find my data file that I'm trying to bundle ahead of time. I even searched my entire hard drive on the Mac and still couldn't find said data file.
The data file isn't an image, if it matters. Just raw binary data. How do I set it up so that this file goes into the proper documents directory at compile time, so that I can read it using the SpecialFolder.MyDocuments prefix? Thanks.
You can't. You can include files in your app bundle, and then at startup copy them from the bundle into a user folder. But this won't happen automatically.

MVC4 App "Unable to load DLL 'libmp3lame.32.dll'

I am trying to use the NAudio.Lame library in an MVC4 application and am getting the error:
Unable to load DLL 'libmp3lame.32.dll': The specified module could not be found.
I added the library via NuGet. I was able to get the library to work fine with a Windows Forms application, so I believe the problem is specific to MVC4.
I tried the advice from the library author here:
https://stackoverflow.com/a/20065606/910348
The problem turns out to be that the native DLLs (libmp3lame.32.dll and libmp3lame.64.dll) cannot be found because the current directory that the web server process is executing from is not the website's bin folder (where the DLLs reside) and the search path does not include the bin folder.
What you need is to add the bin folder to the PATH environment variable, which will enable the LoadLibrary API call to locate the DLLs.
Here's a method you can call that will do this for you:
public static void CheckAddBinPath()
{
// find path to 'bin' folder
var binPath = Path.Combine(new string[] { AppDomain.CurrentDomain.BaseDirectory, "bin" });
// get current search path from environment
var path = Environment.GetEnvironmentVariable("PATH") ?? "";
// add 'bin' folder to search path if not already present
if (!path.Split(Path.PathSeparator).Contains(binPath, StringComparer.CurrentCultureIgnoreCase))
{
path = string.Join(Path.PathSeparator.ToString(), new string[] { path, binPath });
Environment.SetEnvironmentVariable("PATH", path);
}
}
Place that in your controller and call it right before you create the LameMP3FileWriter instance. It might work if you put it in Global.asax.cs and call it from Application_Start(). Try it and let me know if it works there.
I've put a Wiki article about this on the project site here.
Add the following line to the web.config for your site. This will prevent IIS from copying files and executing your site from a temporary folder (native dlls do not get copied apparently). The downside is that you will have to restart the application pool every time you push changes to your bin folder because IIS will lock the files there.
<system.web>
<hostingEnvironment shadowCopyBinAssemblies="false" />
...
</system.web>
Most probably IIS just can't find native dlls. Be sure to place native dll files into one of Windows DLL search path locations.
I made it work by having the LameDLLWrap assembly as a separate one, rather than embedding it in the main assembly. Since I know that my target system is 32 bit, I can afford having a single assembly -- this is simpler for me than playing with PATH on the hoster's machine.
Here's what I did:
Cloned the source
Checkout the Experimental branch
Removed the embedded assemblies from the main project
Set the reference to the wrapper to Copy Local
Recompiled everything

How to access photos/video folder of iOS and copy file to applicaton folder using Marmalade

I need to open iphone folder like photos/videos.
I need to show content of that folder to user and when user selects a file from that I need to copy that particular file from that folder to my application folder.
Let me know what APIs I need to use for that.
Also let me know whether this is feasible or not using Marmalade.
have you tried looking at the s3eImagePicker example? under examples\s3e\s3eImagePicker

Running a packaged exe in the same folder as the installed firefox add-on

I have read this thread, and some other
How to run a local exe in my firefox extension
The problem is, at deployment and using firefox 4.0.1, if I install the .xpi extension, the xpi is put inside the \Profiles...\extensions as ****.xpi, which is a compressed format
All the solutions assume that the the extension is put in a folder, thus they are accessing the folder as is, which I cannot do
for example this guy says
//**** get profile folder path ****
var dsprops = Components.classes['#mozilla.org/file/directory_service;1']
.getService(Components.interfaces.nsIProperties);
var ProfilePath = dsprops.get("ProfD", Components.interfaces.nsIFile).path;
//**** initialize file ****
var file = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(ProfilePath);
//**** append each step in the path ****
file.append("extensions");
file.append("guid");
file.append("sample.exe");
guid in my case is installed as {f13b157f-b174-47e7-a34d-4815ddfdfeb8}.xpi which cannot be accessible this way
First of all, please do not locate files like this - you are making lots of assumptions about the directory structure of the Firefox profile, any of those could turn out false in a future Firefox version (or even in uncommon extension setup scenarios). See Reference a binary-component to js-ctypes instead for code to locate a file in your extension install directory, simply replace components/linux/myLib.so by sample.exe and execute uri.file.
Second: that's a scenario where packed XPI installation won't work unless you want to extract your executable into a temporary file before running it (which will be complicated). Windows doesn't support running executables from ZIP archives. So you need to add <em:unpack>true</em:unpack> to your extension's install.rdf to ensure that it is installed as an unpacked directory.
The best answer is to get flashgot Firefox addon , which contanins already executable inside , and is open source , and , study it , this is great way to observe how this actually works. Based on this , You can make as much as You wish addons with executable inside , or add entire program as FF addon. I hope this helps , even if this answer is rather than outdated.

Resources