0

I am writing an Android app and I need to do something I would consider incredibly simple, yet am having the hardest time figuring out.

How can I obtain the path to a JPG file so that I can upload it to a server?

The file is snoopy.jpg and is in the res/drawable folder

I've tried:

    File sourceFile = new File("android.resource://com.appname.something/drawable/snoopy");

But, that's not a file

I've tried:

    File sourceFile = new File("drawable/snoopy");

and that doesn't work.

I tried putting snoopy.jpg in the root of the app directory and attempting:

    File sourceFile = new File("/snoopy.jpg");

And that still didn't work.

Any help would be greatly appreciated!

Brian Weinreich
  • 7,692
  • 8
  • 35
  • 59
  • I am not an Adroid programmer but does not something like: `File sourceFile = new File("/res/snoopy.jpg");` work? – Eypros Jun 10 '14 at 19:57
  • 1
    Resources are not stored as individual files, however you can create a file from resource data, see ["How can I write a Drawable resource to a File?"](http://stackoverflow.com/q/10174399/2991525) – fabian Jun 10 '14 at 20:02

1 Answers1

2

Try this :

InputStream is = getResources().openRawResource(R.drawable.snoopy);

You can open an InputStream from your drawable resource using the above code. Also, before doing any file operation, you need to check if file.exist() and if it returns false then you need to create a file via f.createNewFile();

android_Muncher
  • 1,057
  • 7
  • 14