0

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:

  1. 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.
  2. 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?

PWJ
  • 83
  • 10
  • `File dir = new File(context.getExternalFilesDir("/File"), "/fonts/downloadedFont.ttf"); ` That is no directory but a font file. Change to `File dir = new File(context.getExternalFilesDir("File"), "fonts");` – blackapps Apr 09 '21 at 12:23
  • File fontFile = new File(context.getExternalFilesDir("File"), "fonts/downloadedFont.ttf"); – blackapps Apr 09 '21 at 12:26
  • @blackapps I have tried making both corrections that you mentioned (First one was my mistake in giving the code), and the issues still persist. – PWJ Apr 09 '21 at 14:10
  • Please change as i suggested. – blackapps Apr 09 '21 at 14:14
  • `request.setDestinationUri` No. you should use request.setDestinationInExternalFilesDIr(). – blackapps Apr 09 '21 at 14:16
  • @blackapps I have also tried that approach: `request.setDestinationInExternalFilesDir(context, "/File", "/fonts/downloadedFont.ttf");` This also runs and downloads the file, but when it gets to the `if(fontFile.exists())` condition, it says false the first time. – PWJ Apr 15 '21 at 04:56
  • You did not make the change i suggested in first comment. – blackapps Apr 15 '21 at 05:02
  • You should use a broadcast receiver which will be triggered when the download manager is ready. Then you can use file.exists(). But better use the uri you get from the manager. – blackapps Apr 15 '21 at 05:04

0 Answers0