Android - Making typeface from file URI doesn't work

179 views Asked by At

Edit: Whoever gave an -1 to this post, explain how would I improve it instead. Stop the toxicity.

I want to make a font preview fragment in my app (getting the .tff file from Intent and setting a TextView's font to that font). Here is how I tried to do this:

private void chooseFont(){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(intent, 1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data == null) return;
    if (requestCode == 1) {
        System.out.println(getFileName(data.getData()));
        if (!getFileName(data.getData()).endsWith(".ttf")){
            Snackbar.make(chooseFontButton, "Only .ttf files are supported in MIUI.", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            return;
        }
        fontPreview.setTypeface(Typeface.createFromFile(data.getData().getPath()));
        //The line above is giving the error. 
        //I also tried creating a new File object with data.getData(). Same result.
        theTheme.font = new File(data.getData().getPath());
        fontName.setText(theTheme.font.getName());
        viewModel.setSelectedTheme(theTheme);
    }
}

However, this didn't work. Here is the error:

java.lang.RuntimeException: Font asset not found /document/primary:MIUI/open-sans/OpenSans-ExtraBold.ttf

What should I do?

0

There are 0 answers