ACTION_VIEW and FileProvider - how to open html file from sdcard in browser

Viewed 125

I could not find a smart solution to my problem and that is why I decided to write to you good people. I have a question for your regarding FileProvider and ACTION_VIEW, namely, I save the html file this way:

    File sdcard = getBaseContext().getExternalFilesDir(null);
    File dir = new File(sdcard.getAbsolutePath() + “/MYDIR/“);
    dir.mkdir();
    File file = new File(dir, “MYFILE.html");
    File recordFile = new File(sdcard.getAbsolutePath()+ "/MYDIR/" + "MYFILE.html");
    FileOutputStream os = null;
    try {
        os = new FileOutputStream(file);
        os.write(returnedText.getText().toString().getBytes());
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

its path: storage/emulated/0/Android/data/com.myapp/files/MYDIR/MYFILE.html

This is how I call my html file:

File sdcard = getBaseContext().getExternalFilesDir(null);
Intent intent = new Intent(Intent.ACTION_VIEW);
File f = new File(sdcard.getAbsolutePath() + "/MYDIR" + "/MYFILE.html");
intent.setDataAndType(Uri.fromFile(f), "text/html");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|
        Intent.FLAG_ACTIVITY_NO_HISTORY|
        Intent.FLAG_ACTIVITY_NEW_TASK);
Intent.createChooser(intent, "File Browser");
startActivity(intent);

I want to call this MYFILE.html via Intent and open in HTML Viewer, I get this error:

ERR_ACCESS_DENIED

So I decided to try FileProvider, but I get a completely different path. What should I do, any specific idea??? Thanks in advance.

My FileProvider:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
    name="external_files"
    path="." />
</paths>

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.myapp.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>
0 Answers
Related