I am developing a mobile application by using Lucene. I have a folder which includes index files. In the application, I want to use these index files for searching. Lucene Library expects path of this folder to read. I placed this folder in the assets folder. But I can't get this folder's path. I checked Android Storage Documentation. But I could not find any solution to add existing folder to external storage. If I would add them to external storage, I can get the path with getExternalFilesDir(). I am working by using emulator.
Directory dir = FSDirectory.open(Paths.get("path of index folder"));
IndexReader reader = DirectoryReader.open(dir);
searcher = new IndexSearcher(reader);
Above code section works well in a Java SE application since I can give the path of folder directly. But In android I am working by using emulator. How can give path of index folder in android? Where should I place this folder to reach in the application?
You put them into the folder into your assets, then copy the assets to the file system on first run. Then you can pass the location you wrote them to to Lucene.
Fair warning: I tried to use Lucene and couldn't a year ago, because it was using IO functions that weren't ported to Android. Now that Java 1.8 exists on Android maybe they have been, but if not you're going to have additional problems to solve.
Related
My Android-APP creates text files that have to be read by other apps.
I originally chose the following directory, which no longer works:
Environment.getExternalStoragePublicDirectory((Environment.DIRECTORY_DOCUMENTS))
Right now I'm trying the directory:
getFilesDir()
The problem here is that no other app can access this folder.
I'm puzzled as I simply don't know which directory I can use for an exchange between apps.
I created a simple file write/read app in Eclipse and have successfully tested that it could read what it wrote. The structure is fairly simple. I use FileOutputStream to write a samplefile.txt using openFileOutput and a FileInputStream using openFileInput to read it back. The only minor thing is that I don't know where is the file on my physical hard drive? Can anybody point me to where I could find the file in Windows?
If you dont give a absolute path the file will be creataed in aplication directory.
This is /data/data/packge-name-of-your-app/
e.g:
/data/data/com.example.test/
See more http://developer.android.com/training/basics/data-storage/files.html
Go to DDMS perspective, select the emulator which you are using, go to package explorer tag ,then you will get a list of folders hierarchy.
Go to /data/data/
Now you will see a list of packages installed, search for the package name of your app and expand it.
Here you will see the files which your application created when it was running.
P.S : You cannot find this file on the physical drive of your computer,because it is present in the storage of the emulated android virtual device. This storage cannot be directly accessed by you.
I am working on a web app i have java files in it which uses certain files.I want to specify these files using relative path in java so that it doesn't produce mobility issue.But Where should i place a file in a web app so that i can use relative path.? I have tried placing the files under source package, web folder, directly under the web-application.Please help.Thanks in advance
The simplest way to get the current directory of a java application is :
System.out.println(new File(".").getAbsolutePath());
Like that you can consider the given path as the root of your application.
Cheers,
Maxime.
Read the file as a resource. Put it somewhere in the src. For instance
src/resources/myresource.txt
Then you can just do
InputStream is = getClass().getResourceAsStream("/resources/myresource.txt");
Note: if you are using maven, then you are more accustomed to something like this
src/main/resources/myresource.txt
With maven, everything in the main/resources folder gets built to the root, so you would leave out the resources in your path
InputStream is = getClass().getResourceAsStream("/myresource.txt");
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.
I am trying to open a file for an Android app I am making. I tested the code that opens this file on a regular Java project, and it opens fine. However when I use this code in an Android Java project I get an java.io.FileNotFoundException error.
File file = new File ("list.plist");
Now the list.plist file is in the parent directory of the project. This file will be included in the app as its the only one it will be using.
I guess I am used to Xcode where I can just place the file anywhere in the project and I can access it without a problem.
How am I supposed to structure this path?
Thanks :-)
I've used this but it also did not work.
File file = new File(context.getFilesDir().getAbsoluteFile(), "list.plist");
Firstly, you need to ensure the file you are using is packaged within your app - such as in the assets directory.
Secondly, Android automatically compresses plist files based on the extension. As a workaround, change the extension to something Android will not compress. These extensions include:
".jpg", ".jpeg", ".png", ".gif",
".wav", ".mp2", ".mp3", ".ogg", ".aac",
".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
".amr", ".awb", ".wma", ".wmv"
(for full article, see dealing-with-asset-compression-in-android-apps)
You need to put this file in the assets dir and not in the parent folder. Any files in the parent folder need not be packaged with the app. Moreover, the structure of your files on a phone is not the same as you see in eclipse.
You should use "res/raw" or "assets" directory to include a raw file in your Android project.
see: http://developer.android.com/guide/topics/data/data-storage.html
I added the list.plist to the assets folder and renamed it list.mp3 to avoid compression issues.
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("list.mp3");
This successfully opened the file without problems, which now allows me to parse the inputStream.
Thanks to Phil for giving sending me in the right direction.