How to get absolute path to a file packaged with the apk? - android

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.

Related

How to provide a text file within an Android app?

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.

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.

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.

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

Accessing image files + file browsing in Android

I'm using HTC Dream and trying to read the images stored in the "default" album folder of this phone. But I've yet to found out what's the folder path to those images file.
When I mount the phone to my comp, some of the default images are in:
/E0FD-1813/DCIM/
and the photos taken using the phone are stored in
/E0FD-1813/DCIM/100MEDIA/
But using those paths, accessing the images throws a "No Such File" error.
So
do you know what's the path to that image folder? What would be the path patterns (if any) in other Android phones?
I don't need to set any additional permission to access those files, do I?
Do you know how I can build a "file browsing" widget in my activity? I've searched and seems like there is no such widget and I've to install app that does file browsing. In any case, how to incorporate that as a file-browsing view in my current activity?
Thanks for your kind advice!
But I've yet to found out what's the folder path to those images file.
There is no single folder.
do you know what's the path to that image folder?
There is no single folder. Use the MediaStore content provider.

Categories

Resources