0

I want to write a number of html files on my PC. I then want these files bundled into my app installable, so when the app is installed on the smart phone, it already has these files stored locally. I then want to use a button in my existing app to select 1 of these files and then to have this file opened up for viewing in a separate web browser (outside of my app). I have tried using code like this below and it works fine for running html pages from external sites:

val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))

startActivity(browserIntent)

However if I try and publish from a file on my Android like Uri.parse("file://android_assets/test.htm") then my app crashes. Can anyone suggest how I can publish html files from my Android app in a new browser, I would prefer to use something like Intent ACTION_VIEW? I have this working with webview but loading the html page inside the app really degrades the user experience.

  • `in a folder that will be local on my Android smart phone` Hmmm. What do you mean? Which folder exactly? You wanna serve from assets? – blackapps Jan 20 '22 at 06:43
  • I understand that I would save local data to file://android_assets. In here I will put a termsandconditions.htm file. A user would click a button in my app and then this .htm file could be published via a web browser outside of the app. – david sutton Jan 20 '22 at 08:16
  • `file://android_assets` Such a path does not exist on an Android device. And how do you want to save local data? With your app? Then you need a valid storage path to begin with. You should better tell what you wan and have in mindt. If you talk about `android_assets` then it seems you want to use the `assets` resource folder at compile time. – blackapps Jan 20 '22 at 09:48
  • Thankyou for your questions. I am not fixed to using android_assets, I am happy to generate these files and put them anywhere on the smart phone at all, as long as they are bundled in with the app so when users come to download and install the app they also have these files without having to go elsewhere or to the web to collect them (imagine no internet access). Perhaps it will help if I add more detail of my use: – david sutton Jan 20 '22 at 11:07
  • 1. user opens app 2. user selects 'show me terms and conditions' button 3. The html document containing the terms and conditions is then opened outside of the app in a separate web browser 4. as the app is still open the user can use other functions in the app 5. should the user wish to close the web browser they can and as it is separate to the app they can close it and the app will still be running – david sutton Jan 20 '22 at 11:11
  • `in with the app so when users come to download and install the app they also have these files ` Well then apparently you have those files on your pc. Then you have to use assets directory to bundle them with your app. – blackapps Jan 20 '22 at 11:57
  • `The html document containing the terms and conditions is then opened outside of the app in a separate web browser` It is much easier to add a WebView to your app as WebView can show files from assets. If you do not want to use a WebView then read my Answer. – blackapps Jan 20 '22 at 11:58
  • You should change the subject of your post as it is not true and very confusing. Better: `Can I launch and view html files stored in assets resource of my Android app....` – blackapps Jan 20 '22 at 12:02
  • Thanks. I used webview originally but it degraded the user experience with the app interface. I will investigate your Answer for a solution outside webview. Many thanks for your help – david sutton Jan 20 '22 at 12:10

1 Answers1

1

Use FileProvider to create a nice content scheme uri for your html file.

Or if you placed your html files at design time in your project assets folder then your html file is in assets resource when your app runs and you can display it in a WebView component. Webview load html from assets directory

If you do not want to use a WebView in your app use your own FileProvider/ContentProvider to serve files from assets.

You could also copy the file from assets resource to local storage and then use FileProvider with ACTION_VIEW to serve your html.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • Thankyou. I understand now, about what types of files you can put into the android_assets folder so they become bundled with your installable and resulting limitations. I have gone back to using webview which works for me at least for html text and text. Thankyou – david sutton Jan 23 '22 at 17:44