So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw in my eclipse project,
and com.alex.testing is my package name.
Still, when I try if(file.exists()) , the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
put the file in assets folder and pick the file from there
Now use Context.getAssets().open("jazz.pdf") and pass the resulting InputStream into PDf parser library
Ok, to access resources from current application you can use something like,
Uri path = Uri.parse("android.resource://<you package>/raw/<your_file.pdf>");
OR
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" R.raw.<your_file.pdf>);
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in /asset directory then copy it to any public access area then view that file from that path.
//if your are stored in SDcard your location would be
"data/data/com.alex.testing/res/raw/jazz.pdf"
//you read resources from raw folder thru the below code
InputStream inputStream = getResources().openRawResource(R.raw.jazz);
byte[] reader = new byte[inputStream.available()];
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);
I'm new to android development and I am working on a little project. What I am having some issue with is getting access to preloaded files.
In my app, I have an XML file that I preloaded (I just simply put it in my src folder in a package). How do I access them in my classes? I need to get a File object pointing to this file so that I can use it as I would I/O files. It seems like this should be trivial, but alas I am stuck.
Lets say the file is located under: com.app.preloadedFiles/file1.XML
I've tried something along the lines of this, but have had no success:
URL dir_url = ClassLoader.getSystemResource("preloadedFiles/file1.XML");
FIle file = new File(dir_url.toURI());
I solved this in my app by getting an InputStream to the file -- something like:
myContext.getAssets().open(fileName);
//read the data and store it in a variable
Then, if you truly need to do File related opterations with it, you can write it to a private (or public) directory and do your operations from you newly written file. Something like:
File storageDir = myContext.getDir(directoryName, Context.MODE_PRIVATE);
File myFile = new File(storageDir + File.separator + fileName);
//then, write the data to the file and manipulate it -- store the name for access via File later
My application is mostly c++ (using NDK) so I use fopen, fwrite, etc. standard functions to create and game save files and write into them.
When I use fopen("game.sav", "wb"), it appears that it's being created at path
/data/user/10/com.my.game/files/game.sav.
My app is multi-user. So I want to have a separated folders where users store their save-files. And instead of the path above I'd like to have paths like
/data/user/10/com.my.game/files/user0/game.sav,
/data/user/10/com.my.game/files/user1/game.sav, etc
My app's frontend is in Java, and when new user is being registered, I want to create a folder /data/user/10/com.my.game/files/user0/. But I don't know how to do it, because
final File newDir = context.getDir("user0", Context.MODE_PRIVATE);
results in path being created at /data/user/10/com.my.game/app_user0 that's a different path.
It is possible to create folders at /data/user/10/com.my.game/files/ and how ?
Simple way to do it, this code you can change it suit many conditions. If you know that your path is different from what getFilesDir() gets you then you can create a File first of all by using a path that you know and the last 2 lines of code will still be same.
File file = this.getFilesDir(); // this will get you internal directory path
Log.d("BLA BLA", file.getAbsolutePath());
File newfile = new File(file.getAbsolutePath() + "/foo"); // foo is the directory 2 create
newfile.mkdir();
And if you know the path to "files" directory:
File newfile2 = new File("/data/data/com.example.stackoverflow/files" + "/foo2");
newfile2.mkdir();
Both code works.
Proof of Working:
I need to access myfile.txt file using FileReader in Android , please suggest me where to add the text file in Eclipse. I tried it adding it in Resource and Asset but I am getting File not found issue.
FileReader fr = new FileReader("myfile.txt");
Even
File ff = new File("myfile.txt");
File Supports only the below listed parameters
FileReader Supports only the below listed parameters
Note: I want solution for this issue , only with FileReader or File
The directory would be /res/raw/ this is where you put all your extra resources.
you can refer to it using getResources().openRawResource(resourceName)
and check here Android how to get access to raw resources that i put in res folder?
EDIT:
how can i edit the text files in assets folder in android
in short
the easiest way would be to copy the file to external directory then do your stuff there
link is here
Android: How to create a directory on the SD Card and copy files from /res/raw to it?
One thing to mention - prior to 2.3 the file size in the assets cannot exceed 1MB.
hope it helps abit
That's how I obtain my file from the SD card, perhaps this can be some use to you.
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File options = new File(getAppDirectory(), "portal.xml");
}
The getAppDirectory method used in the bit of code looks like this :
private String getAppDirectory() {
return new String(Environment.getExternalStorageDirectory().toString()
+ "/foldername/foldername/");
}
After this bit of code I also make sure the file exists and what not before I attempt to read from it.
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.