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.
Related
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")
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.
I have text files in the raw folder that I would like to use in my app. I am using ListFragments and depending on what item is selected I want to load that files text in a new layout.
I can properly load the text fine but I would like to be able to scan the file previously so that I can have the first line of the file be the title that is displayed in the ListView. I have a method that determines the number of files in the raw folder and adds the names to the ArrayList.
Here is my method:
private void listRaw() {
Field[] fields = R.raw.class.getFields();
for (int count = 0; count < fields.length; count++) {
myArrayList.add(fields[count].getName());
}
}
The problem i am having is setting up a scanner. I do not know how to tell the scanner to scan this specific file. I tried making a new File object and used the Scanner(File) but I do not think I am declaring the file name properly nor if this is even the best way to get this done. Normally if you know the file name you can just simply do:
Scanner fileReader = new Scanner("filename");
but in this loop I never actually have the file name set.
I have text files in the raw folder that I would like to use in my app
I am assuming that "the raw folder" means the res/raw/ resource directory in your project.
I do not know how to tell the scanner to scan this specific file
That is because there is no file. The file exists on your development machine. The representation of the raw resource at runtime is just that: a raw resource. Under the covers, it's effectively an entry in a ZIP file.
You need to get the R.raw value for a raw resource, then use getResources().openRawResource() to get an InputStream that you can pass to a Scanner.
Unless you have different editions of these raw resources for different configurations (e.g., language), you might find it easier to work with the assets/ directory and AssetManager for packaging text files with your app.
I need to check if a txt file exists on assets folder, and if exists I need to read the text of it and put into a string.
I saw some approaches on Google to open files from assets but I can't find one that gives you the possibility to check if the file exists and then read a string from it (because is a text resource).
You can just handle the exception properly when the file isn't found like this :
try {
InputStream is = SplashscreenActivity.this.getAssets().open("yourTextFile.txt");
} catch (IOException e) {
// catch your exception properly when the file isn't found
}
In short, you must already suppose to know if the file exists in assets or not. Because assets folder is packed up with files once you build your .apk file and can never be changed.
So, its pretty lame to verify if the file exists or not. It will only contain those files which you have added yourself.
More over, if you want to use R file to help you locate the file name, then simply put your files in a folder called raw. Its an alternate to assets but the files saved in this folder are referenced in the R class file - making it easier to get hold of the resource.
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