I created Assets folder by right click App > New > Folder > Assets Folder.
I also created a working webpage in another editor which created index.html
along with css folder with index.css and js folder with index.js and
subfolders for both for some css and js libraries.
I wish to bring those over to Android Studio so that I can load the
index.html in a webView. What is the correct way to do it?
Here is the absolute path of both index.html and MainActivity.java files
from my Mac:
/Users/fred/Documents/a/mobileWebApps/r/index.htm
/Users/fred/Documents/App1wv/app/src/main/java/com/example/fred/app1wv/MainActivity.java
Thanks
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView)findViewById(R.id.main_WV);
wv.setWebViewClient(new WebViewClient());
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
ws.setAllowFileAccess(true);
String text = null;
String path = "file:///android_asset/";
try {
text = convertStreamToString(this.getAssets().open("index.html"));
} catch (Exception e) {
e.printStackTrace();
}
wv.loadDataWithBaseURL(path, text, "text/html", "utf-8", null);
}
public static String convertStreamToString(InputStream is) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/5c653eed-1a5c-4298-91f0-626bfdfdba6c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.