-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Bug] [Android] Intermittent error in production: Font not found /data/user/0/com.package.whatever/cache/font.ttf #15012
Comments
Do you have any additional code that does something with the fonts? How do you load/add the fonts to your project? Do you see this only on Android or also iOS? |
Not doing anything special, just followed your guide here: https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/ I think the problem is that
The OS can clear the cache folder at any time, so we need to handle when the file doesn't exist by just creating the font file again. (https://developer.android.com/training/data-storage/app-specific#internal-create-cache) I can submit a PR if you could talk me through your preferred fix (was thinking maybe move all the calls to CreateFromFile in FontExtensions into a method somewhere and call LoadFont again inside a catch?). |
But if it exists, we return it and else we try to move it there, that is already happening, right? Xamarin.Forms/Xamarin.Forms.Platform.Android/EmbeddedFontLoader.cs Lines 18 to 27 in 41a6548
|
Yes, but it appears that
Maybe bypass the cache on Android and call |
Closing this as I've been unable to reproduce it. Will continue trying and open a new issue if necessary. |
thats rare but coming now and then, I have just got for android. Font is there its working fine but I dont get why this error is occuring even not very often. I am using the latest version of the XF and AndroidX libraries. Android: 6.0.1
|
I'm going to reopen this, I have a reproduction scenario. I'll put something together and add it here. I encountered this today during development on an emulator, so I've got a full stack trace with line numbers now, which is more useful. My current theory is that the Dictionary used to cache the font files in The font files are being deleted while the app is running, the emulator was low on disk space when the crash occurred. It's likely that the few users who are being affected have a low amount of storage available on their device. |
Seeing this issue too by adding a new font to a currently installed app. The font doesn't load and the error message detailed above is shown in the output. |
Hi Team, any update on this issue? |
Also would like updates on this. |
My app is showing crashes from this issue as well |
I'm also getting this crash reported in AppCenter for few Android devices, |
started seeing these crashes too (Font asset not found) - just from one user at the moment (Galaxy A22s 5G, on Android 13)... this is in code that has been in production for years and as far as I know we've not had these failures before. Maybe google have changed something recently which is causing these to be more common? Edit: noticed we also had another on a Galaxy Tab A7 on Android 12 |
Any ETA on fix? |
I am also facing same issue |
now getting more and more of these crashes. |
This is a critical issue for me also, as it makes production apps crash constantly. Please resolve it. Migrating to MAUI would be a lot bigger job, and out client won't will to pay for that, just because there's a framework bug in Xamarin.Forms. |
the number of these crashes keeps going up for us... |
This is also a growing issue for us. |
Try running your app in a really lowend emulator with low resources. For us it turned out that one of our ViewModels started a resource-heavy task on the main thread, which resulted in an out of memory exception. This crash about missing a font was just the simptom of this memory problem. (Possibly the device went out of memory, and to try to handle that, it cleared the cache also.) When I move that task to a background thread, the problem disappeared. |
Similar experience for us - only has become an issue relatively recently for apps that are otherwise years old. Happening on Android 13 / Galaxy Tab devices... |
Any update on this? |
Add added the next code to my Android project: [assembly: ExportRenderer(typeof(EmbeddedFont), typeof(MyEmbeddedFontLoader))]
namespace MyProject.Droid
{
public class MyEmbeddedFontLoader : IEmbeddedFontLoader
{
public (bool success, string? filePath) LoadFont(EmbeddedFont font)
{
var tmpdir = Xamarin.Essentials.FileSystem.AppDataDirectory;
var filePath = Path.Combine(tmpdir, font.FontName);
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists && fileInfo.Length == font.ResourceStream.Length)
return (true, filePath);
try
{
using (var fileStream = File.Create(filePath))
{
font.ResourceStream.CopyTo(fileStream);
}
return (true, filePath);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
File.Delete(filePath);
}
return (false, null);
}
}
} |
@ArtjomP that code is essentially exactly the same as the default font loader already in xamarin forms. how is that supposed to change things? [pardon me if i've missed something obvious - this is a genuine question, not a critique] |
It uses Xamarin.Essentials.FileSystem.AppDataDirectory instead of temp folder. Android should not remove files from Xamarin.Essentials.FileSystem.AppDataDirectory. |
Ah yes that makes perfect sense, thanks for clarifying. |
Although I found way to reproduce this issue.
|
I have no issues working in emulators, but as I start to test my app on an actual device, I also get such error. Host: MacOS, Windows |
Description
This exception is being reported intermittently, where the application seems to be unable to find the font file in the cache folder.
Error message:
Font not found /data/user/0/com.package.whatever/cache/font.ttf
Stack trace
Steps to Reproduce
Expected Behavior
The app won't crash and fonts will always be loaded successfully.
Actual Behavior
I've seen this error reported twice in the last couple of weeks, both with similar stack traces as above.
Basic Information
Environment
Show/Hide Visual Studio info
Build Logs
Screenshots
Reproduction Link
Workaround
The text was updated successfully, but these errors were encountered: