How to provide a text file within an Android app? - android

Where do I put a text file that I want deployed with my app such that I can provide within my app a path to that text file. I have an included JNI library which will take the text file at that path and perform actions on it. So in other words, I don't think I can just put it in my assets folder without reading the file and resaving it to SD card or something (as I don't think you can reference an assets file directly by path right?). Is there a way around this?

You can place files in res/raw/ and read them as raw resource files/streams.
See https://stackoverflow.com/a/15934525/5734895 for reading raw resources.

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.

Android: Where can I put a file in the same app and load it by its path

What I'm trying to do is put a file in the app (Like in assets or resource).
But I need to load it by its path "I don't want to use the assets manager .. etc", this is because I'm using a lib need the file name (Not stream).
Note1: I know I can save the file to SD card then use it, but I'm trying to find a way without doing this (Load it directly).
Note2: I tried "file:///android_assets" but it didn't work.
You cannot access asset files via File API because it stays in your APK, which is in fact just ZIP file. Files in asset folder are accessed through native asset API where it is handled as ZIP stream. You can see Asset.cpp and its method createFromCompressedFile.

Loading JPEG from RAW resources

I need 6 large images in my app. And I don't need the quality and alpha channel provided by PNG. Is there a way to embed JPEG files into the raw resource and unfold them into SD card at first launch of the program? It will save me 3-4 MB in my APK. Thanks!
It's very easy. Put your file in res/raw filder and use:
InputStream is = getResources().openRawResource(R.raw.image);
BitmapFactory.decodeStream(is);
You can simply open the stream and do whatever you want with the data.
I think I don't understand your question: It seems that you want to hive off image files at first run...
Anyway, I suggest to put the files in the assets folder and not in the resource. You can access the assets as a file system and copy them to any (permitted) location.
Have a look here:
Difference between /res and /assets directories
and here (look at assets):
http://developer.android.com/tools/projects/index.html
EDIT:
My answer suggests to use assets instead of the resources but, you can't modify your apk at runtime, look here:
how we can remove a file from the assets folder at runtime in android?
Nothing prevents you to put JPEG images in the resource folders, or in the assets folder if you don't need the R.drawable.MY_IMAGE thingy.
However, the images would still be included in your APK, and cannot be removed from your application package even after you copied them to the SD card.
The only way is to download the images separately from a web server on your application first launch.
The Google Play Store also provides some facility if your application needs big files, but that seems a bit of an overkill

Accessing assets in subdirectories in android?

I'm reusing my iphone resources, I have myriads of resources which have many subdirectories
I'm reusing much of native code aswell, I understand that to open assets we MUST provide relative path for ex if I have file name test1.png in assets/subdir1/subdir2/subdir3/test1.png
I have to mention whole of the relative path, As I already said I'm using much of native code so is there any way I can just access asset files using just the filename without full relative path "test1.png"? Or if this is not possible is there anyway we can logically structure our resources but the folder name actually doesn't exist physically.
I understand that to open assets we MUST provide relative path for ex if I have file name test1.png in assets/subdir1/subdir2/subdir3/test1.png I have to mention whole of the relative path
With AssetManager and methods like open(), you would not have the assets/ portion of the relative path. In your example, you would use open("subdir1/subdir2/subdir3/test1.png").
As I already said I'm using much of native code so is there any way I can just access asset files using just the filename without full relative path "test1.png"?
Of course not. You might have 10,000 files named test1.png in your entire assets/ tree -- we need to know which of those to open.
Moreover, AFAIK, native code has no access to assets.
Or if this is not possible is there anyway we can logically structure our resources but the folder name actually doesn't exist physically.
Whatever you physically put as your directory tree in assets/ in your project is what you will need to use when accessing them through AssetManager. If you do not want to have to deal with subdirectories with AssetManager, do not create subdirectories inside of assets/

Categories

Resources