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

0

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"

c#
xamarin
xamarin.android
asked on Stack Overflow Mar 13, 2019 by Mir Karam

2 Answers

0

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 ();
     }
answered on Stack Overflow Mar 13, 2019 by Samir Bhatt • edited Mar 13, 2019 by Samir Bhatt
0

In my case, I had a Xamarin Forms project. I had included a resource in the iOS project which the xaml page was referring to, but forgot to include it in the Android project.

answered on Stack Overflow Jun 8, 2020 by Gautam Jain

User contributions licensed under CC BY-SA 3.0