My app depends on 3 different files. I already put the two larger ones as expansion files which seems to work fine. Now I don't know where to put the third one (small file). I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
I tried this here for the raw folder
myClassifier.loadModel( pathToExpansionFiles + "/File1",
pathToExpansionFiles + "/File2",
R.raw.File3);
However the return value of R.raw.File3 is an integer, but the function myClassifier expects a string that is the path to File3. Has anyone an idea how to do that?
I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
There is no path to it. That is a file on your development machine. It is not a file on the device. It is merely an entry inside your APK file.
but the function myClassifier expects a string that is the path to File3
Either switch to some library that works with an InputStream (from getResources().openRawResource()) or make a copy of the data to a local file using that InputStream yourself.
Related
I try to use OpenNLP in an Android application with eclipse, and I imported the 4 JARs into the libs folder.
It's probably a stupid question.. but where should I put the model files "en-pos-maxent.bin"? I can't find anything regarding the path anywhere
I try to run the code contains this line:
POSModel model = new POSModelLoader()
.load(new File("en-pos-maxent.bin"));
I tried putting the en-pos-maxent.bin inside a new folder within the project("tagger/en-pos-maxent.bin"), inside the libs folder, or simply give the path where the en-pos-maxent.bin is put when downloaded("Users/ariel/Downloads/en-pos-maxent.bin"), it always give me the error: POS Tagger model file does not exist path: (the path I typed in)
Could anybody help please?
When you use new File("filename") the file is expected to be in the current working directory, I don't think that's different on Android. You can use System.getProperty("user.dir") to get the current working directory. It's by default the directory you start the program from. You could also specify a full path like new File("/full/path/filename") instead.
In android the right way to add file to the application (inside APK) is to put into the assets folder. Then you can't get File object from assets folder but, you can create that File using a buffer or in you case just get it into the InputStream. For example if you will create models folder into the assets folder (assets/models), your code will look like this:
AssetManager am = getAssets();
InputStream inputStream = am.open("models/en-pos-maxent.bin");
POSModel posModel = new POSModel(inputStream);
So, I've seen many posts on this, but none has helped. I want to work with the assets folder, for organizational purposes. The images below show where the 'assets' folder is, along with the app.imi options of where it should be. I have tested this, and in the >exploded-aar/com../appcompat-v7/19.1.0/assets ... the files inside this seem to work. I can call on the .mp3 file and load and play it correctly (Though a few times it did not recognize it. Even when it was inside a folder, inside of the directory.)
In the src/main/assets folder, all the files are ignored. Is there a specific way I have to call this? In other words, must I also link the actual folder it is it? src/main/assets/music to load up the riseofcc.mp3?
I do not have 10 reputation to post images, so sorry for only links.
EDIT:Also I am using Android Studio 0.5.7
ANSWERED:
Thanks for Xavier Duchrohet for explaining exploded-aar is for the application's dependencies. I have learned that when calling upon a file inside your asset's folder using the code:
AssetManager assetManager = getAssets();
InputStream inputStream = null;
AssetFileDescriptor descriptor = assetManager.openFd("sound/explosion.ogg");
You must include the folder's parent inside the arguement.
Android Studio's app.iml has the asset's directory (src/main/assets) inside of its configuration's options.
Normally, to include sound files, I create a folder called raw, inside the app / src / main / res.
That is, I right click on the folder res and from the menu that appears, choose: New / Android Resource Diretory. Renaming in the window that appears, the Directory name field values for raw.
How it works for me, I hope this might have helped him.
I need to get Absolute path to Folder in Assets.
Some like this for sd-card:
final String sdDir = Environment.getExternalStorageDirectory() + "Files";
What i do incorrect?
First I try to get path (in green ractangle) this way but I alwase get "False".
Then I comment this block and try to get path from getAssets().list();
But I get 3 folders witch I see first time.
I want to make massive like this "green" but I need to use files from assets:
Look the image
Help me to get absolute path to my Files folder.
I'm not exactly sure what you're trying to do, so I'll try to cover the bases, and maybe this will help.
If you are just trying to get a list of what's in your assets, then
use getAssets().list("Files"). (You have to use the subdirectory,
because of this).
If you are trying to get an absolute path to your assets directory
(or any subdirectory), you can't. Whatever is in the assets directory
is in the APK. It's not in external storage like on an SD card.
If you are trying to open up files in the assets directory, use
AssetManager.open(filename) to get the InputStream. Here,
filename should be the relative path from the assets directory.
EDIT
I'm not sure what you mean by "massive", but if you want to load the file black.png from assets instead of the SD card, then write this:
// must be called from Activity method, such as onCreate()
AssetManager assetMgr = this.getAssets();
mColors = new Bitmap[] {
BitmapFactory.decodeStream(assetMgr.open("black.png"));
// and the rest
};
Assets are stored in the APK file so there are no absolute path than your application can use. But, I would suggest to take a look at file:///android_asset. It might fits your needs. Here is a good example on how to display an Asset in a WebView.
In my raw folder I have a number of folders, how can I target these, Ive tried a path var as below but no luck:
String path = "R.raw.myFolder";
mSoundManager.addSound(1, path.myFile);
I do not understand why you have folders inside your raw folder. That folder is there to store the media you need. Do not over complicate the project by having to reference a folder name on top of the file name. the code below is for accessing files straight out of the raw folder.
Try giving this a go:
mSoundManager.addSound("android.resource://"+getPackageName()+ "/" + R.raw.filename);
I am not sure if this is what you are looking for but this is what works for me for all my raw files.
I left out a piece. Code is now correct.
I have few html files in assets folder of my application. My application loads these files depending on the device language. When I check for the existance of the file it say does not exist, but when I load that file using browser.loadUrl(filename), it loads it fine.
Following code will help you to understand my problem:
String filename="file:///android_asset/actualfilemname.html";
File f = new File(filename);
if(!f.exist){
filename = "file:///android_asset/newfile.html";[Everytime it loads this file even though I have actualfilename.html in the folder]
}
browser.loadUrl(filename);
[it loads the newfile.html but not actualfilename.html]
You can't use File for resources. You'll need to use the AssetManager for that.
(In the off-chance that File does handle resources, which I don't think it does, you'll have to convert the path to a URI first, for example using URI.create(). File(String) expects a path, not a URI.)
Is this the exact code you are using? you probably want to be calling f.exists() not filename.exist().
Edit: try working with the AssetManager instead of hard coding your file path. My best guess is that the file path you are using is not exactly how it supposed to be.