I am working on an android app in which I need to detect the language of user's input text.
So using Stackoverflow I found a recommendation of using a java library called langdetect which requires reading languages profiles.
I was able to create a simple plane java project, by adding a directory (folder) inside the java project called "profiles" which contains all the languages profiles.
I couldn't make this work in android since the only 2 ways I know of accessing files in android either by adding the desired files inside "assets" or "rec/raw" but I keep getting error saying file not found.
The method from langdetect jar file that requires reading profiles is the following
String path = "profiles";
DetectorFactory.loadProfile(path);
the above code works in plain java.
Any help guys.
I used the following
Uri.parse("android.resource://com.my.package.my.app/raw");
file:///android_asset
classLoader.getResource("profiles");
and many others in the same style.
The problem is that I don't need to access specific file per say, the only thing I need is a path to the folder that contains the languages profiles, the folder contains 53 files for 53 languages.
path is relative. It does not make sense in Android. You should refere the Internal/External storage with Absolute Path. If you put your data inside the sdcard, for instance, you can retrieve the Absolute Path with Environment.getExternalStorageDirectory(). If you want to embeded your data inside the apk, using the assets or raw folder, you need the AssetsManager in the former case, getResources() in the latter.
Related
Edit: Clarity - the main .pl file loads, it's all the subfiles that it has been told to load which don't load. (all the consult('subfile.pl').)
I have a Java project that uses tuProlog. It calls a theory as:
Theory theory = new Theory(":-consult('main.pl').");
engine.setTheory(theory);
This is as per the manual.
file.pl exists in the same folder as other prolog files.
Inside of main.pl, I have further
consult('otherfile.pl').
statements to load additional files (several).
The folder structure is:
src/main.pl
src/Prolog_Files/otherfile.pl (multiple)
src/main/java/JavaStuff
I can't get the engine to load the theories that I've told it to consult inside of the main file.pl
I have tried: giving it absolute path instead of just filename.
moving files around.
I'm wondering if there is something about usage of tuProlog I'm not understanding?
The theory works when loaded with:
Theory theory = new Theory(new FileInputStream(url_of_file)).
However, this is causing me issues when building the jar, as it can't find the file location.
Am I attempting to load the file correctly? Are my consults inside of the main .pl file correct?
Could someone please post an example of how this should be done if not? The manual doesn't elaborate very much on this topic.
Thanks
The manual is slightly outdated in parts - it says to use consult/1, whereas elsewhere it states that consult/1 is deprecated, whereas include/1 is the replacement.
Secondly, when using 2p.jar, it reads Prolog files from the Project root as its root. When creating a jar, 2p.jar cannot be inside of the project jar. They should be in relative folders, and 2p.jar reads Prolog files with the location of 2p.jar as root. It doesn't seem that it can read inside of project jar.
Hopefully that's clear enough!
Hi I was wondering how to access files in the project folder in java/maven, i have thought about using src/main/resources, but i have been told it is a bad idea to write to files in this directory, and should only be used in configuration for the project, So i have created a new non source folder but i was wondering if there is a way to access this file in java without giving an absolute path, as i need to use it in different env. any help or other suitable suggestions would be great here thanks. i will be writing to the files at runtime.
EDIT:
i am using this to acces the file:
private static final String ACTUAL_VALUES ="verification/actualCounterValues.csv";
where verification is a folder i have created in my project
As i understand you, your goal is to access a file from within your app, without hardcoding a relative path because you're going to run it on different environments.
The first thing you may have to solve is to decouple the file-reference from your app - because if you move your app it must adapt to the new environment.
You may solve that by putting an absolute file-reference into the system-environment (which is accessible through the System.getenv() method). Another way could be to deliver the file-path as command-line-argument.
After that you have to specify when which running instance of your app will access the file. If your application runs separated in the maven and a production environment - everything is fine with using relative paths in the maven-project. The production-app will then generate and use its own file in its environment.
If they must share the file, you have to provide physical access from both environments to that file-path, after that you may access that file with separate absolute paths (delivered through cli-args or system-properties) or with "hard coded" relative paths, which access a file-link (which point to that absolute file).
But i must discourage you from using any hardcoded configuration-specific variables for ongoing maintenance reasons.
And if these two application-instances will access both one single file, you should also be aware of possible concurrency difficulties (especially in the filesystem - see that post).
I am developing an application with Android/Java. I need to attach a list of audio files into my java library. So that the developer can use the library and play the audio files. How can I attach the audio files into my Java library and how to call them as file path to play them?
If you are creating an Android Library, you can put your audio file in the raw folder. People using your library can refer to it using com.example.yourlibrary.R.raw.sample_audio_file.
I hope this is what you were looking for. The file will be embedded when the library is created.
If you have specific audioFiles then add them to the special resource folder and refer to them as getResourceAsStream . If you want user to use their own files - provide the variable for the folder path and make a filter to use only specific files, i.e *.mp3.
If they are stored in specific resources folder, why not to create special Accessor class for the developers to use it? Like you will have a *.jar file that will contain files in the separate resource folder with the Accessor to work with them.
Android uses a static resource file R. This file (at least in eclipse) is automatically updated when ever you add new id's of any sort. How can I create/implement the same feature in a normal java application? Is it as simple as just writing an xml parser and just updating the resource file after the xml is modified?
In a way, yes. You need to create a custom build script/program which runs at the start of each build (before anything else), scans your resource folder files (and if they are XML files it needs to read in the XML files and parse out the string resources or whatever from those), then write it all to a Java file in some manner (e.g. R.string_name = "string value").
Make sure the XML files aren't actually packaged in your .jar, since all that information will be stored inside your Java resources file now.
For things which aren't XML files you could just store the filename as a string in the Java resources file.
You didn't specified the type or the use of the resources. I don't know android, but I'll try to help; If you just need to access some resource in your application you can use properties or resource, there are some differences see this other question Properties vs Resource Bundle
I want to know how I can use ResourceBundle.getBundle() in an Android application given that I use it in my Java applications. For example:
I have a properties file, "MyOrg.properties", which I've included in a JAR file named "MyOrg.jar". The path information in the JAR file associated with "MyOrg.jar" is "myorg\" (this is on a Windows system). I build my Java application using "myorg.jar" and note that the following line of code works as expected, which is that it loads the file "MyOrg.properties" from "MyOrg.jar" as a java.util.ResourceBundle:
ResourceBundle resources = ResourceBundle.getBundle( "myorg.MyOrg" );
Next, I place a modified copy of the file "MyOrg.properties" on the file system in the directory "c:\myorg", which is on my Java application's class path. I now rerun my Java application and note that the Resource.getBundle() returns, as expected, a bundle for the modified copy of "MyOrg.properties" (the one that is on the file system).
The behavior I've just described is what I would like to be able to accomplish with an Android application. However, ResourceBundle.getBundle() throws a MissingResourceException; it fails to find "MyOrg.properties" in either the JAR file or as a stand-alone file.
So I have two questions:
1) - Is it possible for an Android application to retrieve a resource from a JAR file using ResourceBundle.getBundle() (or any other way for that matter)?
2) - Is it possible for an Android application to retrieve a resource from a file using ResourceBundle.getBundle()?
Regarding 2): I'm using the nbandroid plugin with NetBeans 6.7.1 and I've placed copies of "MyOrg.properties" on the file system as follows prior to building my Android application:
MyProject
-- build
-- classes
myorg (directory contains "MyOrg.properties")
...
src
myorg (directory contains "MyOrg.properties")
you need to make sure the properties file makes it to the .apk file. your best bet is probably in res/raw/ or assets/. see also PropertyResourceBundle's constructors (since it's easy to get hold of an InputStream).