Monday, February 1, 2010

[android-developers] Re: Discover resources?

Lo and behold, R.xml.class.getDeclaredFields() was picking up
Android.R, because I was not in the activities package. I guess there
are no xml resources in Android.R. So there is some hope for this
method yet.

I don't think the AssetManager will work, though. It's returning
"images", sounds, and "webkit" for empty path, and nothing for "xml"
path.

Nathan

On Feb 1, 6:54 pm, Lance Nanek <lna...@gmail.com> wrote:
> Weird, I dropped a settings.xml in res/xml in a quick test project and
> R.xml.class.getDeclaredFields() worked just fine. I tried running from
> Eclipse and also exporting to an APK, installing, and running that -
> both worked. Tested on an Android 1.6 G1 phone and an Android 2.1
> emulator. Maybe there are different ways to build that actually do
> strip them out?
>
> Screenshot:http://imgur.com/yWU3I.png
>
> Code (http://pastebin.com/f4f1ee8bd):
> public class TestReflectionOnR extends Activity {
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>         TextView text = (TextView) findViewById(R.id.text_view);
>         text.append("field count: " + R.xml.class.getDeclaredFields
> ().length + "\n");
>         for (Field f : R.xml.class.getDeclaredFields()) {
>             f.setAccessible(true);
>             try {
>                 text.append(f.getName() + ": " + f.get(null) + "\n");
>                         } catch (IllegalArgumentException e) {
>                                 throw new RuntimeException(e);
>                         } catch (IllegalAccessException e) {
>                                 throw new RuntimeException(e);
>                         }
>         }
>     }
>
> }
>
> On Feb 1, 6:19 pm, Nathan <nathan.d.mel...@gmail.com> wrote:
>
>
>
> > How does one go about listing the resources that are available in an
> > application?
>
> > For example, I would want to find the names of all files under /res/
> > xml.
>
> > Reflection on the R class didn't seem to work since the identifiers
> > are static constants. R.xml.class.getDeclaredFields() doesn't return
> > any fields.
>
> > The AssetManager didn't seem to work either because these are not raw
> > assets.
>
> > Does anyone have ideas or examples?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment