0

I am a beginner to Android App Development. I am creating a Code Editor app (that would allow developers to edit and run their html files) by using Java along with html, css and javascript (via WebView). I could easily accomplish the edit part but the problem is with executing any html file.

My first try was to use iframe with a src pointing directly to the file in sdcard-

<iframe src="file:///storage/emulated/0/<file-name>.html"></iframe>

But with this code, the problem is that the app immediately crashes when this iframe loads. Not only iframes any kind of request sent to a route in sdcard from the webview immediately crashes the app.

I find various solutions on internet but they all just add a JavascriptInterface that can be used to display images on webview. But that's not the case with me as you know the code that I want to execute is dynamic and must be able to access any kind of file that is accessible to a html file in a normal browser and also that the entire sdcard, though can be referenced in html codes, cannot be copied to assets folder. With these circumstances, all answers on internet are no helpful for me.

Summary- I want to view the output of any html code, stored in sdcard of users, in such a way that the output appears similar to what it would in a browser, that is by using Java and WebView Please help.

Edit- I want the solution to be by use of a iframe, which can be completely accessible to entire WebView.

  • Does this answer your question? [load somefile.html file from internal storage to a webview of an app](https://stackoverflow.com/questions/14624567/load-somefile-html-file-from-internal-storage-to-a-webview-of-an-app) – Yogi Feb 09 '23 at 12:02
  • Which exception that you did not catch lets your app crash? Look in LogCat. Android version of used device? Who put that file there? And the path you mentioned is not on a removable micro sd card. – blackapps Feb 09 '23 at 12:59
  • `Summary- I want to view the output of any html code` ?? It looks you wanna display a html file in an iframe. – blackapps Feb 09 '23 at 13:01
  • Have you done all this?: https://stackoverflow.com/questions/8955228/webview-with-an-iframe-android – blackapps Feb 09 '23 at 13:02
  • Ans use File.exists() and File.canRead() on that file before you start the WebView. – blackapps Feb 09 '23 at 13:05
  • @blackapps, Using iframe crashes the app. And the suggested answer won't work because I also want to change the content of the file a little bit before opening it in WebView. I want to change its content to add a script reference to eruda.js- to display a console window, which a very important feature of my app. – Hit and Run Feb 09 '23 at 13:14
  • @blackapps I am checking it with File.exists() but always if any request to sdcard goes from WebView, the app crashes. – Hit and Run Feb 09 '23 at 13:15
  • Also by sdcard I mean the storage/emulated/0 directory, i.e. the shared storage – Hit and Run Feb 09 '23 at 13:17
  • How do I use LogCat? – Hit and Run Feb 09 '23 at 13:19
  • Unlike the suggested answer, my iframe can access the http://www.google.com the problem is with file:///storage/emulated/0 – Hit and Run Feb 09 '23 at 13:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251755/discussion-between-hit-and-run-and-blackapps). – Hit and Run Feb 09 '23 at 13:27
  • `I am checking it with File.exists() ` Did not i mention File.canRead() ? And didnt i ask who put that file there? And didnt i ask for Android version of used device? And thye LocCat is part of Android Studio IDE. open it an read. – blackapps Feb 09 '23 at 13:44
  • I am using Android 10. I must try File.canRead() and inform you about that. But what do you mean by who put that file there? I don't understand that – Hit and Run Feb 09 '23 at 13:47
  • Well that file is on your device. How did it get there? Who donnit? How? – blackapps Feb 09 '23 at 13:48
  • That file is not in my device. I have just given it for example. I mean that users will create .html files and save it in shared storage by using my app. And those same files are meant to be opened. I never mean by this that I am going to access a non-existent file from iframe. Anyways that's also not a valid file name – Hit and Run Feb 09 '23 at 13:54
  • So you are testing on a device without that file and then your app crashes? And you will not first put a file there? I cannot follow you. – blackapps Feb 09 '23 at 13:57
  • No no, that's not what I mean. That was just an example. Now i am actually looking for "file:///storage/emulated/0/untitled.html", which is present in my internal storage – Hit and Run Feb 09 '23 at 13:59
  • I am never accessing any non existent file. That thing is just for example. I wanted to access any existing file in shared storage, so I named it like that. – Hit and Run Feb 09 '23 at 14:01
  • What special thing did you do to make it possible for your app to read from and write to external storage on an Android 10 device? (as one special thing has to be done only for Android 10 devices). – blackapps Feb 09 '23 at 14:03
  • Added READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE and MANAGE_EXTERNAL_STORAGE permission in AndroidManifest.xml.and asked for those permission in MainActivity.java. The permissions are successfully got, it is visible in the app info of my app. But I don't know any special thing – Hit and Run Feb 09 '23 at 14:15
  • Ohh, i just see in google. You might be talking about requestLegacyExternalStorage. I have done it. I actually can save files, read files and do everything in external storage but only in java. When it comes to WebView, nothing works. I cannot access any html file and JavascriptInterface is not the solution – Hit and Run Feb 09 '23 at 14:19
  • Sorry, because of being new to Android. I thought that requestLegacyExternalStorage to be for all devices so I denied to have done anything special – Hit and Run Feb 09 '23 at 14:23

0 Answers0