1

I have data.xlsx file in Assets folder and need to get its path to apply some library.

Found several solutions here but neither works for me. For example - File Not Found Exception In Xamarin program

I tried to use file:///android_asset/data.xlsx but here is no file too.

I checked the path with: System.IO.File.Exists(filePath);

So how to get path to this file? Maybe I have to place it in different folder?

Update: possibly I can solve it this way: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

But I can't find where to place it.

Update: this question is about Java and this is unclear how to use this solution in Xamarin.Android: How to get the android Path string to a file on Assets folder?

5
  • try to use * file://android_asset/data.xlsx * instead of file:///android_asset/data.xlsx Commented Sep 4, 2016 at 9:37
  • @MiladYarmohammadi, same result. Commented Sep 4, 2016 at 9:46
  • 1
    see stackoverflow.com/questions/8474821/… Commented Sep 4, 2016 at 11:40
  • Does your library not have an overload that has a stream as parameter instead of a filename? Commented Sep 4, 2016 at 12:07
  • @InfernumDeus If the library does not accept a Stream and only a file-based Uri, then copy the file, via an Asset stream, from your app bundle to a local dir (CacheDir, etc...) and pass that file path to your library method... Commented Sep 4, 2016 at 16:50

3 Answers 3

3

Looks like the file isn't included in the APK. Ensure that the Build Action of the file in the Asset-folder is set to AndroidAsset.

Sign up to request clarification or add additional context in comments.

2 Comments

Good point but yes, it was automatically set to AndroidAsset.
Do you get a valid stream if you open the asset via a the AssetManager?
1

in general using Xamarin.android , you can use this code :

private string _fileName {get;set;}

      public string FileFullPath
      {
         get
          {
            return Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures),_fileName);
          }
      }

Hope it helps.. :)

2 Comments

Can i somehow refer to any different directory than Android.OS.Environment.DirectoryPictures? My file is input data so this sounds a bit inappropriate.
Android.OS.Environment.GetExternalStoragePublicDirectory is now deprecated
0

Looks like it's impossible.

Closest solution is to write file to Cache.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.