Search code examples
c#xamarinxamarin.android

In Xamarin.Android getting resource id "Android.Content.Res.Resources+NotFoundException:" error occur


I want to get id(int) for audio file from Asset folder but it give me error.

var File_Id = Resources.GetIdentifier("my_file.ogg","Assets", PackageName);

Above line of code execute in android Service it give error,

Error Log:

03-13 14:03:50.047 W/ResourceType(13170): No package identifier when getting value for resource number 0x00000000 Unhandled Exception: Android.Content.Res.Resources+NotFoundException: Resource ID #0x0

this file "my_file.ogg" is in "Asset" folder ,build action set to "AndroidAsset"


Solution

  • The asset can be read using the AssetsManager. Try below code snippet to read file from asset.

    Android.Content.Res.AssetFileDescriptor afd = this.Assets.OpenFd("File name");
    

    Updated Answer :

    The following code snippet will help to play media file directly from the Asset folder.

    Android.Content.Res.AssetFileDescriptor afd = this.Assets.OpenFd(fullPath);
         if  (afd != null )
         {
             player.SetDataSource (afd.FileDescriptor, afd.StartOffset, afd.Length);
             player.Prepare ();
             player.Start ();
         }