Adding file to .apk - android

My program needs large .txt file to be stored on SD-card (so, I want to redistribute it with .apk, without creation from the program). How I can attach the file (created on PC) to .apk?

You can save it in /res/raw folder
If you save your file as yourfile.txt
InputStream inputStream = this.getResources().openRawResource(R.raw.yourfile);

As far as I know you can open a .apk with zip and add files. Where is no magic :)

It might be slightly easier to use the AssetManager. Just save your files in the assets/ directory (as opposed to res/raw/). Then you can access them directly using their filename. http://developer.android.com/reference/android/content/res/AssetManager.html describes how to access the assets.

Related

How can i insert a .bin created on desktop file into apk

I have code for creating an internal file, there is random algorithem that create the data stored in it and i want any app to have the same file with the same binary data in it.
so i need to make the file on my desktop and add it to internal files some how.
my question is what do you think is the best way to do it.
i thought to locate it in my project, read it, and write it to internal files.
the problem is, i dont know where to locate my file in android studio so that it will be included in the external files and then where to read it from.
thanks. =]
hope i made myself clear.
Put it in src/main/assets/.
You can then access your file with AssetManager and do whatever you want with it.
From the Android Developers website:
main/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.
You need to move that into the assets folder. From there you can refer to the file.

How to put a *.cfg file to the same directory as the *.apk file?

I am really new to android devices, we have got an app that needs to read a *.cfg file from the same place as the apk. So the question is:
1) When i attach my mobile-phone to the computer, can i put it from the explorer? If yes, how?
2) Is there a mechanism in Android, that looks for some kind of include directories
when opening a Program?
It would be great if you could give me a hint.
You'd want to drop the *.cfg files into the assets/ folder. Then the app will have access to those raw *.cfg files during runtime.
From Managing Projects
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.

How to write in a file declared in res/raw?

I have a file stored in res/raw, and I would like to know how to wite into that file.
I tried getResources but it returns an inputStream.
Not possible. If you need to copy an asset to the device store the file in the assets directory. Then use the AssetManager to stream in and copy the asset so you can write it to disc or application cache.
You shouldn't be writing your application resources. Instead, try copying the resource to your writable data directory and modifying it there. There a number of methods on Context that will help you find an appropriate directory, depending on your needs.

Can i move a file in an asset folder to an asset sub-folder?

i have a file in the asset folder, and when i press a button in my program i have to move that file in a sub-folder(run-time). Can it be done?
i have to do this:
assets/file.txt -> assets/aaa/file.txt
No your assets are compiled into your apk file. Consider storing a persistent preference marking whatever you are trying to do instead.
No, you can't do it. You can't write anything inside the assets folder programmatically. Better option is to use internal storage. You can use Files Folder in the internal storage. See getFilesDir() in the docs.
Reason: Once you have an .apk file it is read only, so you can't add, delete or modify in it,
Alternate:
though you can use the sqlite DB for the purpose... where you can save file in hierarchy using blob type and you can modify it through queries...

Android: How to find the absolute path of the assets folder?

I need to get the absolute path of the assets folder in my app as I want to serve the files within using a webserver and it needs the absolute path. Is this possible?
I would have thought there might be something like this:
String rootDir = getAssets().getRootDirectory();
but there's not.
Any help appreciated, cheers.
Is this possible?
No. There is no "absolute path of the assets folder in [your] app". Assets are stored in the APK file.
In select cases, such as URLs supplied to a WebView, you can use the special file:///android_asset base URL to reference files in your assets.
You can always copy files from the assets directory in the APK to a folder on the device, then serve that folder.
As mentioned, Android assets cannot be accessed with absolute paths in the device file system. So whenever you have to provide a filesystem path to a method, you're out of luck.
However, in your case there are additional options:
I want to serve the files within using a webserver and it needs the absolute path.
Needing the absolute path is only true if you want to serve the file as a static file with the default mechanism a webserver provides for that. But webservers are much more flexible: you an map any path in an URL to any data source you can access: files, databases, web resources, Android resources, Android assets. How to do that depends on the web server you use.
For example, you can define for youself that any URL starting with https://example.com/assets/ should be mapped to the assets folder of your Android APK. You can then open the asset as an InputStream and serve the content to the webserver's client.
If you have any asset like PDF file stored inside assets folder then get its path using below line:
/assets/file_name.pdf

Categories

Resources