Is it possible in Android, to manually add a file inside a project and then, modify it? Example:
"I have a test.txt file in the following path: "app/src/data". I would like to make a method to write a given String in the test.txt file."
Is that possible? I been looking everywhere, but can't seen to do such an easy task.
If you mean modifying files inside the APK itself then it's not possible. Besides, the folder structure you see in the project is not the final structure on the APK (just unzip your APK, it's a .ZIP really): Fpr example, the source directory is all compiled into a classes.dex. The res/ directory is compiled and fully copied ...
Take a look at How to write files to assets folder or raw folder in android?
and https://developer.android.com/training/basics/data-storage/files.html
You can read raw files stored in /res/raw, or assets stored in assets/ , but you cannot modify stuff inside the APK itself.
What you can do is create and modify as many files as you wish from the different places Android gives to any app, such as:
CACHE directory (context.getCacheDir() -> /sdcard/Android/data/your.package/cache
External files (context.getExternalFilesDir() -> /sdcard/Android/data/your.package/files
Arbitrary directories in the SDCARD.
Related
With Java NewIO it should be possible to have a single copy method that handles:
file (existing) to file
folder (and all its contents) to folder
zip to folder (a.k.a. extraction)
folder to zip (a.k.a. compression)
...
...
A jar/zip file is a filesystem just like my harddisk. Maybe one would need a little conditional execution in an universal copy method, but it should be possible, right? Maybe even with Filefilters and other stuff on top?
And if it is possible it should exist? I did some extensive Google search and got back nothing. There is a myriad of examples showing how to do one "type" of copy. But there is no universal copy method. Or is there? And if not, what would it look like?
I am trying to create a folder which contains uploaded files. I have seen some examples that saving files into the absolute folder such as D:/tmp or Home directory. My question is I am trying to put my uploaded files into a folder which is inside the project folder.
I also tried to get project folder using like this :
public static String getProjectFolder(){
return System.getProperty("user.dir")+"/";
}
Result become something like this, and file not found exception inside the Html file!
http://localhost:1234/Users/USER_NAME/Documents/java/PROJECT_NAME/upload/genres/action.jpg
What is the best way to add folder into my project? Or should I use hardcoded folder for it ?
You need to set the folder that you want to upload files to as the static files folder using staticFiles.externalLocation().
The only folder the client can reach, either for getting content from OR uploading to, is the folder you set using the static files location API staticFiles.externalLocation(). You can see also an example for that here.
When I run my server locally, I define it to be:
staticFiles.externalLocation(System.getProperty("user.dir") + "/src/main/resources");
This is regarding what your question.
You might run into an issue though. Because now you probably have two locations that you want public:
The resources folder (with all the JavaScripts, CSSs, etc. Typically /src/main/resources)
Upload folder (to let the user upload files)
Having both of these the same location is not a good practice. I managed to resolve that by creating a symbolic link called files inside the public folder.
abcmbp:resources abc$ pwd
/Users/abc/dev/wspace/proj1/src/main/resources
abcmbp:resources abc$ ls -l files
lrwxr-xr-x 1 abc staff 27 Mar 3 21:20 files -> /Users/abc/appUploadFolder/
Then, in the post handle of the uploading path, I give this folder as the destination, and this way I have a separation between resources and uploaded files.
I have once again had trouble using JWI Wordnet in Android. The only way to access it, is to create a URL or File pointing to the directory where the files it needs are located. http://projects.csail.mit.edu/jwi/api/index.html I have downloaded the files I need, and added the jar into my project. My error is when I try to create the Dictionary object used to access wordnet. I don't point to a valid directory. Which makes sense, but I just don't know how to do so pointing the correct way. Basically, the folder name is "dict", I can place it into either raw or Assets (Although I read something about files in assets needing to be < 1 mb, the files inside the folder are greater than 1 mb) I need to create either a File or URL that points to said folder. How would I go about doing this?
So, you cannot create a file from raw or assets directly, as they are not stored as files, but you can copy/unpack them somewhere else, and make a File object using the copy. Android: how do I create File object from asset file?
I'm trying to load words into an arrayList from a txt file in the assets folder in my android program, and looking at the api, it says that AssetManager.list(String path) will "Return a String array of all the assets at the given path."
Does that mean that if I have a text file, words.txt, and I put in "words.txt" as the parameter for AssetManager.list(String), that it will return a String array of all the words in the txt file?
If not, how would I go about reading a txt file into an array in my android program?
I couldn't find a definition for "asset"
I'm using eclipse.
EDIT:
I'm not just asking what an asset is, I'm also asking about a specific method in the api and how to load strings into an arrayList
Android offers one more directory where you can keep files which also will be included in package. This directory called /assets. The difference between /res and /assets is that Android doesn’t generate IDs for assets content. You need to specify relative path and name for files inside /assets.
assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
Source
Asset Usage Example
assets is place where you can normal files which you want to read in application as some point whenever needed. You save your files/directory's. And read in your application from input streams but you cannot write.
As far res concerned, they resource files which you can refer directly in your code. like strings, array of strings,images (drawables),style(android specific like css in html) and there is raw folder which stores all files binary and generates id for it.
After searching 1 hour i do not found any solution to my problem.
I want to move a file from sdcard to assets folder and also overwrite the existing file in assets folder (both file are sqlite database have same name with just a little difference in data )
??
Actually, The behaviour of the android .apk file is read only so the directory structure which are in that apk file follows the same, So no one can write this files or make changes on that at runtime,that's why You can't make a changes in /asset folder at runtime. you can read file from it (means copy files which are available in asset to any internal storage or /sdcard but not write it to vice-versa)
Its apply on all the directories which are build in .apk files. As per docs.
Assets folder are not Writable it is only readable so you cant copy from sdcard to assets, yes you can copy from assets to sdcard
If you want other apps not to access your database then what you can do is
Download the database from website. create a temp database also in app directory.
getDatabasePath() this will give you the path of the dummy database you have created now replace this with the one you downloaded.
PS.: User and apps having root access still able to access it and you cant do anything in that scenario