1

For example, this would be the path to a file on the external memory...

String path = Environment.getExternalStorageDirectory().toString()+"/myApp/face.jpg";

But is there also such a path to the Asset folder?

Cheers!

P.S.

"file:///android_asset/1.png"

Gives a file not found.

Luther
  • 11
  • 1
  • 3
  • Why do you want to do such a thing? BTW, assets is a folder intended to put some raw files. Strings should be placed in res/values and accesed via R autogenerated file. – Mister Smith Sep 05 '11 at 07:52
  • no, no, the assets folder will have PNGs. The string is the path I need to it, such as "file:///android_asset/1.png" ( only it doesn't work ). Since my app at first was working with files from an ExternalStorage. – Luther Sep 05 '11 at 08:06
  • That is not the way to get an image from resources. And images should be placed on res/drawables. This way you can get the drawable from R.drawable.myfile. If you want to retrieve a generic file from assets, use the AssetManager class. If the resources were on res/raw, you should use `Activity.getResources().openRawResource("myfile.txt");` – Mister Smith Sep 05 '11 at 08:22
  • I have just found a possible duplicate of this question: http://stackoverflow.com/questions/4789325/android-path-to-asset-txt-file – Shlublu Sep 05 '11 at 08:58

2 Answers2

3

In case you just need the path for accessibility-reasons you can use

AssetFileDescriptor descriptor = getAssets().openFd("file.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());
DonGru
  • 13,532
  • 8
  • 45
  • 55
2

The path to an asset is

file:///android_asset/[path_under_the_asset_folder]/your_file.ext

If you are getting a FileNotFoundException, your [path_under_the_asset_folder] is likely to be wrong.

Shlublu
  • 10,917
  • 4
  • 51
  • 70
  • there isn't any "[path_under_the_asset_folder]", the file is right in the assets folder, without any subfolders. – Luther Sep 05 '11 at 07:59
  • Are you sure the file is correctly included to the APK? If you have added the image to your asset folder using the Windows file explorer, don't forget to use F5 under Eclipse to refresh that folder, otherwise your resource will not be included to the APK. – Shlublu Sep 05 '11 at 08:07
  • all done. :) but I have read that file:///... would work only for webview. so guess will have to put those files into r.drawable :) – Luther Sep 05 '11 at 08:14
  • Not necessarily. On my side I often use assets for help or EULA files displayed in a `Dialog` box, and this works fine. Did you try the way @Martin suggested? – Shlublu Sep 05 '11 at 08:17
  • In martin's way actually not sure how to turn that readable to a string path. any ideas? – Luther Sep 05 '11 at 08:24
  • Not sure we can get the path from the `AssetFileDescriptor`. Do you absolutely need it? I've also find that post that says basically the same thing: http://stackoverflow.com/questions/4789325/android-path-to-asset-txt-file – Shlublu Sep 05 '11 at 08:57