I am downloading font files from a direct download link and saving it to my device.
The sequence is as follows:
Activity 1 - Press Next
Activity 2 - OnViewCreated, the below code is called to download and set the font face.
Activity 3 - The font type is set to the existing TextView.
I am having two issues with the code:
- When newly running the application, if I go through the steps, the font does not exist in the location and does not get set to my TextView the first time I run through from Activity 2 to 3. If I go back and then return (From 3 to 2 and back to 3), then the program says that the font is available and sets it.
- If I try to delete the folder or the files in the font folder, the font folder seems to be deleted forever and no matter how many times I switch through the activities (Back to 1 or 2), the font does not get downloaded.
This is the download code:
public static void downloadFont(Context context, String url) {
deleteExistingFonts(context);
File fontFile = new File(context.getExternalFilesDir("/File") + "/fonts/downloadedFont.ttf");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Sample Font File");
request.setTitle("Font.ttf");
request.setVisibleInDownloadsUi(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
}
request.setDestinationUri(Uri.fromFile(new File(context.getExternalFilesDir("/File") + "/fonts/Font.ttf")));
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Long downloadID = Objects.requireNonNull(downloadManager).enqueue(request);
if (DownloadManager.STATUS_SUCCESSFUL == 8) {
if(fontFile.exists()){
Typeface typeFace = Typeface.createFromFile(fontFile);
setDownloadedFontType(typeFace);
}
}
}
This is the code to delete the existing folder/files in folder:
public static void deleteExistingFonts(Context context) {
File dir = new File(context.getExternalFilesDir("/File"), "/fonts");
if (dir.isDirectory()){
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
}
Any ideas on how I could solve either of these issues?