14

I have an Android application used to transfer the .png image from asset folder of Android application to my local server. I need to get the path of image in asset folder, pls refer my code below,

String stringPath = "android.resource//"+getPackageName()+"/raw/sample/logout.png";             
File f = new File(stringPath);

If I use the above code I got "File not found exception". So how to take image path in asset folder?

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
sivanesan1
  • 779
  • 4
  • 20
  • 44

3 Answers3

10

Try:

file:///android_asset/raw/sample/logout.png

If you have a Context, you can use context.getAssets().open("raw/sample/logout.png") to open an InputStream on the file.

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
3

Assets and resources are accessible using file:///android_asset and file:///android_res.

 Uri path = Uri.parse("file:///android_asset/raw/sample/logout.png");

 String newPath = path.toString();
Sanu
  • 455
  • 1
  • 6
  • 17
  • Use toString() to convert a Uri to a String. Use Uri.parse() to convert a String to a Uri. – Sanu Apr 07 '14 at 07:39
  • 1
    i use exactly same code of what you give but File not found exception only comes, Actually i pass the file path in a third party method and its only gives that error. is their any method avail to check the above code is working correct or not. – sivanesan1 Apr 07 '14 at 07:54
  • 2
    Same problem you cannot get the absolute path to a file this way. – lostintranslation Jul 01 '14 at 22:06
  • returns FileNotFoundException – Matt Apr 15 '17 at 01:58
0

You could put your mp3 files at : res/raw folder.

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.myringtone);
mediaPlayer.start();