0

I am trying to open a Resource by passing the result of context.getResources().getString(R.drawable.myimage) to another class that is not an activity. context.getResources().getString(R.drawable.myimage) returns res/drawable-mdpi/myimage.png However, when I try and open this file, it throws a FileNotFoundException.

What is the proper way to open a resource outside an activity?

Alexis
  • 23,545
  • 19
  • 104
  • 143

1 Answers1

0

Can you post the code where you try to open the file.

res/drawable-mdpi/myimage.png is a relative path, not an absolute one. I guess you're not opening the correct path by using the relative and not absolute path.

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
  • Yes, here is the code that is in another class that tries to read in the file path. DataSource fds = new FileDataSource("res/drawable-mdpi/checkin.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); How do I get the front part of the path to make it absolute? – Alexis Aug 04 '10 at 12:17
  • I'm not sure if the resources can be read from an app directory other than the assets directory like this. One way might be to use a custom data handler and work with the InputStream from context.getResources().openRawResource(R.drawable.myimage)), for the next step then see: http://stackoverflow.com/questions/2830561/how-to-convert-an-inputstream-to-a-datahandler Not sure if there's another easier way though. – Mathias Conradt Aug 04 '10 at 16:24