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
Related
I am trying to make an app app that will be used to upload a file to the Firebase Storage. I don't want the user to have to choose a file instead i want to package the file with the app. (e.g in the raw/asssets folder).
I have tried several things and I can't make sense of anything. I have no direction. I watched a few tutorials on youtube but all of them use 'choose a file' method.
Any help is greatly appreciated.
You cannot get the absolute the path of the file located in assets. Because those files which are located inside the assets folder does not have the absolute path because they are packaged with the application. But you can use an AssetManager object to get an InputStream on an asset.
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.
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.
I'm working on an Android application that needs to store media (document files like pdf or so for later reading) but the main requirement is that all media files have to be accesible through an absolute path (a physical path on filesystem).
I'd like to avoid copying files to external storage (like sdcard or phone internal memory) so to prevent that if application is uninstalled those files remain in phone (and of course to avoid duplicating the size in kb for each file) and instead to keep files in iny App internal resources folder, but tried "file:///asset_folder" whith no success. As far as I know "file:///asset_folder" only Works for a webview to Access www folder but not for regular files.
I'm not sure if there is any app internal data folder which I can access through an absolute path or if not which is the best way to store App resource files.
Thanks in advance!!
Edit: To make it more clear, the resource files are already bundled with the App, and not written during runtime, and what I'd like to know is where to put them so I can later Access them via absolute path for Reading.
To retrive your app specific data
openFileInput(file_name)
To save your app specific data
openFileOutput(file_name, Activity.MODE_PRIVATE)
Update : Read from asset folder. (InputStream)
getAssets().open(fileName);
getAssets().open(fileName, accessMode);
check the image to where to put asset files
now to make other app readable your files from private data/data directory use content providers.
You can not write data inside asset_folder because it is packed on apk file. You can use sd card or location where your app is installed inside internal memory.
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.