1

I wanted to use Android Assets and followed the tutorial in the official docs.

Have set the asset type to AndroidAssets like it says, but it was throwing exceptions for not finding the file. I have a rooted device so upon inspection I noticed that the files are not actually on the device at all (/data/data/com.company.application/files/SomeFile.txt).

EDIT:

String filename = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "AboutAssets.txt");
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
A--C
  • 36,351
  • 10
  • 106
  • 92
jProg2015
  • 1,098
  • 10
  • 40

3 Answers3

0

Android Assets are stored in the APK file, so you can't find the file using your code. There is no absolute path to those files.

Just use Assets.Open to access assets, exactly like what taught in the tutorial.

Aaron He
  • 5,509
  • 3
  • 34
  • 44
  • Crashes in the same way unfortunately :( Then I read on the forums to try the Path.Combine approach. – jProg2015 Mar 16 '13 at 23:47
  • How come? `File not found` exception? Use the exact same line of ` using (StreamReader sr = new StreamReader (Assets.Open ("read_asset.txt")) content = sr.ReadToEnd ();` in the tutorial. It can hardly get exceptions. – Aaron He Mar 17 '13 at 01:32
  • File not found or part of file path not found. Definitely set to AndroidAsset though. – jProg2015 Mar 18 '13 at 12:02
0

You can try:

AssetManager assets = context.getAssets();
InputStream is = assets.open(filename);

The context Object refer to the Context of Application.

Mazen Kasser
  • 3,559
  • 2
  • 25
  • 35
Franklin Hirata
  • 290
  • 2
  • 12
0

Posted this a while ago and ended up not using Xamarin because I never found a solution. I have recently come back to Xamarin and the latest updates seem to have fixed this issue - the same project now works fine with no changes made.

So if anyone else encounters this - try updating.

jProg2015
  • 1,098
  • 10
  • 40