ResourceBundle on Android in Assets folder
Asked Answered
U

1

5

i´m currently useing ResourceBundles in a pure Java program. Now I want to use the same files inside of an Android app. It works as intended, when the file is placed e.g. at /sdcard/. Is there a way to load them directly from the known android folder structure? e.g. from assets wihtout manually coping them to the sdcard?

This works at the sdcard:

    File file = new File("/sdcard/");
    URL[] urls = new URL[0];
    try {
       urls = new URL[]{file.toURI().toURL()};

    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    ClassLoader loader = new URLClassLoader(urls);
    ResourceBundle rb = ResourceBundle.getBundle("test", Locale.DEFAULT, loader);

This url doesn´t work for the assests folder:

   urls = new URL[]{new URL("file:///android_asset/test/")};

Thanks for any help.

Uralic answered 9/11, 2014 at 20:54 Comment(2)
if you want a way to load file from assets, take a look at this answer [1]: https://mcmap.net/q/125818/-read-file-from-assetsSontich
@Sontich ResourceBunlde can´t handle an InputStreamUralic
A
1

Try creating a resources folder on this path src/main/resources and then put or get your files from there by using ResourceBundle on Android.

It works for me.

Annulus answered 9/6, 2017 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.