views:

154

answers:

1

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?

A: 

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 Lin
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 K
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-datahandlerNot sure if there's another easier way though.
Mathias Lin
also helpful: http://thedevelopersinfo.com/2009/11/17/using-assets-in-android/
Mathias Lin