0

Basically at run time I want to read the entire file line by like. I tried the below and always getting a null. If there is a correct way to do what I need thanks!

InputStream is = this.getClass().getResourceAsStream("/values/strings.xml");

I tried different combinations of the path like just strings.xml and strings and /res/values/strings.xml and it did not work either.

Tony Anecito
  • 347
  • 2
  • 13
  • Why do you want to do that? – Ricky Mo Sep 26 '22 at 06:52
  • 2
    Read xml value or full string file including xml tags ? – Shivanshu Verma Sep 26 '22 at 06:53
  • You can read all the values. Instead of reading the entire file which is not the right thing to do. – Haris Sep 26 '22 at 07:17
  • Ricky - I want to create a copy of the file at run time. Shivanshu I want to read the entire string including tags. Haris, this is for international reasons so I need the names and values from the file. I tried reflection and could only get names and not that plus the values. – Tony Anecito Sep 26 '22 at 17:56
  • I could do a brute force solution and copy the contents of strings.xml to another file and store it where I can get to it easier. But, I was trying to be conservative. – Tony Anecito Sep 26 '22 at 18:01
  • That `strings.xml` file and name don't really exist in any form at runtime. Its contents are basically lumped together with all the other stuff inside `` tags in all the other `res/values/` files then processed by type: `string`s, `color`s, `style`s, etc. I'm not sure exactly what you need for the international, but Android inherently supports resource localization out of the box: https://developer.android.com/training/basics/supporting-devices/languages. If you really do need the file at runtime, you'll have to pack an unprocessed copy, which you can put in `res/raw/` or `assets/`. – Mike M. Sep 28 '22 at 02:12
  • 1
    Thanks Mike I was wondering if that is the case. You confirmed what I suspected also of assets/ and I did not know about res/raw. Many Thanks! – Tony Anecito Sep 29 '22 at 01:59
  • So I am wondering if I can force the loading from that location. So if I have french strings can the "french" locale be read from that directory there or any folder I have created? I read somewhere about using the assets folder. – Tony Anecito Sep 29 '22 at 02:23
  • Yeah, but it won't involve assets; not sure what that method might've been. At runtime, resources are loaded through instances of the `Context` class – e.g., `Activity` and `Service` classes – and a `Context`'s `Resources` holds a `Locale` object that determines which directory to pull from (though those directories don't really exist at runtime; you know what I mean). I'm not terribly familiar with changing the `Locale` there, but I know it varies a bit between Android versions, and can be finicky. I'd start with these: https://stackoverflow.com/q/2900023, https://stackoverflow.com/q/4985805. – Mike M. Sep 29 '22 at 02:54
  • I am trying the second link and working on getting the sample code running. I posted a question to try to get what I hope is the last issue with the example resolved. – Tony Anecito Oct 03 '22 at 05:01

0 Answers0