Create Android tree-uri from directory tree-uri and filename - android

Given a tree-uri obtained from the ACTION_OPEN_DOCUMENT_TREE intent and which references a directory, and given the display name string (e.g. myfile.jpg) of a file that exists within that directory, how do I build a tree-uri that refers to that file?
Conceptually, what I'd like to do is "append" the filename to the directory tree-uri to obtain a valid file tree-uri. Is there a way to do this - preferably efficiently?

Related

Reading JSON or text file in resource folder

I am developing Kotlin app.
I am trying to read text and JSON files which I stored in resource folder(res/raw/file.txt).
Looking at the hierachy of app structure, I put absolute path(../../res/raw/file.txt) for file reader.
Unfortunately, my app cannot find the file.
Can you tell me how I can read files stored in res/raw? If I store file in wrong directory, can you tell me where to store and how to access them?
Thanks
If you want to read a file from res/raw folder you can obtain InputStream by R.raw.yourfile id like this:
resources.openRawResource(R.raw.yourfile)
Or you can open file by Uri:
val uri = Uri.parse("android.resource://com.example.your_package/raw/yourfile")
val file = File(uri.getPath());
Alternatevily you can put your files in assets/ folder and get InputStream like this:
assets.open("yourfile.txt")

How to read raw folder file name which has space between words

I have a few mp3 files in raw folder in which some of the file name have two words.I mean space between the words.Please help me how to read the file name.
That's not a valid resource naming. Replace all spaces with underline character (_):
Original - original file name.mp3
Changed - original_file_name.mp3
This will allow you to access that file through Resources.openRawResource() API.

Can't read file stored in res/raw

I'm unable to successfully get the contents of a public key file that I have stored in my res/raw directory.
My objective is to read the contents of this file into a File object that I will then pass along to another function that requires a File object as one of its parameters.
It's important to note that this file contains an X509 key, which I believe means that I need to read its contents as bytes (not text).
This is in the onCreate method of my main activity:
String filename = context.getResources().getResourceName(R.raw.public_key);
Log.d(TAG, filename);
File publicKeyFile = new File(filename);
if (!publicKeyFile.exists()) Log.d(TAG, "no public key file");
Which yields this output:
raw/public_keyno public key file
What am I doing wrong? Thanks!
My objective is to read the contents of this file into a File object that I will then pass along to another function that requires a File object as one of its parameters.
That is not possible, unless you make your own local copy of that file.
What am I doing wrong?
Raw resources are not files. They are entries in the ZIP archive that is the APK file. If you want a File, open an InputStream on the raw resource, copy its contents out to a local file, then use the local file.

Retrieve new and old path of a renamed file under observing directory

My App saves recorded video files in a specific folder in external directory and saves meta data including the path of every file in database . I need to Observe remove , rename and move of my file in case of keeping database updated with new file path. I read about FileObserver in android. If I give the path of my directory in SD Card for example /storage/emulated/0/my_recordings as input path of my FileObserver, events will be fired for all of my files in the input path and also it will be fired for MOVED_TO event type which i use to observe rename operation of files under my directory, I will get the new relative path of my file in the path argument of onEvent (int event, String path) but how can I retrieve the old file name or old file path of that file to understand which database entry I have to update ? I passed the path of parent directory of that file so I don't have the old file name.
Thanks in advance
There should be a MOVED_FROM event right before the MOVE_TO. The path of the MOVED_FROM event should be the old file name while the path in the MOVED_TO should contain the new file name.
Also make sure that you monitor this event type by explicitly passing it in the flags parameter of the FileObserver constructor.

getting the full path of R.raw folder

I have a binary file in the R.raw folder. Now I am able to
get the file name and can read the file as well, but I need to find out the complete
path of the file in string format.
any help will be appreciated.
So if you are able to get the file name you probably have a File object. If so call this method: http://developer.android.com/reference/java/io/File.html#getAbsolutePath%28%29
I think you should consider storing new files into your application data directory:
http://developer.android.com/reference/android/content/ContextWrapper.html#getDir%28java.lang.String,%20int%29

Categories

Resources