File in asset folder not found

Kalibo 1 Reputation point
2020-12-24T20:07:46.507+00:00

hello guys,

I'm trying to get the path of the json file (which is in my asset file and with -> Build Action: AndroidAsset ) via -> string pathJsonfile = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "barbershop4.json");. But I keep getting the error that there is no such file. Could someone please help me?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2020-12-25T11:57:06.497+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    There is no "absolute path for a file existing in the asset folder". The content of your project's assets/ folder are packaged in the APK file. Use an AssetManager object to get an StreamReader on an asset as Jessie's answer.

    For WebView, you can use the file Uri scheme in much the same way you would use a URL. The syntax for assets is file:///android_asset/... (note: three slashes) where the ellipsis is the path of the file from within the assets/ folder.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Alessandro Caliaro 4,196 Reputation points
    2020-12-24T22:53:56.267+00:00

  3. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2020-12-25T02:18:13.433+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Yes, to access fille in Assets folder in android , you can use AssetManager to achieve this, just as alessandrocaliaro mentioned.

    Suppose I put a google-services.json in folder Assets, then we can use the following code to access it:

        mTextView = FindViewById<TextView>(Resource.Id.mText);  
    
        string content;  
        AssetManager assets = this.Assets;  
    
        using (StreamReader sr = new StreamReader(assets.Open("google-services.json")))  
        {  
            content = sr.ReadToEnd();  
        }  
    
        mTextView.Text = content;  
    

    For more detail , you can check: Reading Assets .

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.