So I have been at it for a while, and I really cannot get it to work. What I want is this:
To access some file outside the project folder through a WebView.
Here is some code that I have been trying to get it working:
String url = "file://///\\myProjectName" + Environment.getExternalStorageDirectory().toString()+ File.separator + "hw.html";
WebView webView = (WebView) findViewById(R.id.WebviewLOAD);
webView.loadUrl(url);
I just get a ERR_FILE_NOT_FOUND with any type of file. For a test-HTML file I created, I get this along with file not found error:
"The webpage at file:///myProjectName/storage/emulated/0/hw.html could not be loaded because"....
I tried removing Environment.getExternalStorageDirectory().toString() but then i just get file:///myProjectName/hw.html along with the file not found error.
So I want to navigate to the right file and be able to open it, but I just can't seem to get it right. I'm open for any suggestions?
So, basically you can't do this. Instead you want to run a local server in the background and make the WebView and server work together.
I did it with Jooby with an SQLite database.
Related
I am an iOS developer assigned a task in Android, so bear with me, I'm a bit green in Android.
I am attempting to load a local html file that is stored in the device download directory in a folder called user_guide. I want the html file to load in the device's browser (not in a webview for reasons outside the scope of this post). I am using the following code to launch the browser:
String downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
String path = "file://" + downloadPath + "/user_guide/index.html"; // + R.raw.test;
Uri pathUrl = Uri.parse(path);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.setData(pathUrl);
context.startActivity(browserIntent);
I obtained the value of path by setting a breakpoint and manually set it in Chrome on my device to verify that is does work and loads the proper file. However, when I try to run this code in the app, I get the following toast message:
Cannot display PDF (index.html is of invalid format)
I'm confused about this message since I am trying to load an html file, not a PDF. Can anyone help me out? THanks!
Try changing "browserIntent.setData(pathUrl)" to
browserIntent.setDataAndType(pathUrl, "text/html")
to explicitly specify that it's HTML.
I found this suggestion at https://stackoverflow.com/a/7009685/10300291.
i have a interactive map folder. i try to create a webview app with local files.
inside the folder are all the files needed to load the map.
gta5online.com/map-interactive
also included is a html file that i tried to open in an app. i just pasted the folder inside the main application folder for android studio.
i tried to put the path in the code as you can see for the string. when i run emulator it says it cannot find the file. what do i need to put there. can someone give me the exact one please? i'm a noob.
the path on my pc is: C:\Users\me\AndroidStudioProjects\MyApplication\interactive\map.html
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url ="file:/interactive/map.html";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings() .setJavaScriptEnabled(true);
view.loadUrl(url);
The best way to reference html content in this case is to put it in an ./assets folder and use the URI file:///android_asset/ to refer to it.
See for example the reply here: How to get URI from an asset File?
To be fair, I haven't tried that since Android Studio became a thing and gradle changed the default project layout -- hopefully that doesn't further complicate matters for you.
I'm having problems loading an external html file into the webview. I've done this before and it should be easy, but for some reason I keep getting Web page not available.
I know the files are in the directory because I placed them myself using file explorer.
String filename = "file:///"+ Environment.getExternalStorageDirectory() + File.separator + "Android/data/com.example/files/test_html2.html";
webview.loadUrl(filename);
I've tried moving the files into root and trying there, I've removed file:// and replaced it with content:// and nothing at all. I have read permissions in the manifest.
Any ideas?
Don't create file:// URLs yourself, as you will tend to screw them up. In this case, I think that you have four slashes after the :, three that you typed in and one from Environment.getExternalStorageDirectory().
Instead, create a File object and use that as the basis:
File f = new File(Environment.getExternalStorageDirectory(), "Android/data/com.example/files/test_html2.html");
webview.loadUrl(f.toURI().toURL()); // or use Uri.fromFile(f).toString() instead
I have a file in the internal storage of my tablet. /myfolder/subfolder/index.html. How can i load this into a webview of an app.
i have tried
webview.loadURL("file:///myfolder/subfolder/index.html");
but it is not giving the expected result. it says web page not available.
I know how to load from the asset folder or from the web/internet. but i need to load local file into webview. is it possible.?
File file = new File("/data/data/packagename/foldername/");
webView.loadUrl("file:///" + file);
I was able to solve my problem by using the following as path:
webview.loadURL("file:///mnt/sdcard/myfolder/subfolder/index.html");
An app cannot access data from the Internal storage stored by another app. Permissions are applied to internal storage that make data written by an application not accessible outside of that application (your app cannot read anything written by another app).
So, if you are accessing a file that is not created by your app, AFAIK, you cannot have access to it.
BTW, you could access the file from the internal storage as below,
webview.loadURL("file:///data/data/com.yourproject.example/files/index.html");
Put your html files in asset folder access the page like given below.
webview.loadURL("file:///"+mContext.getFilesDir()+"/myfolder/subfolder/index.html");
you have to mention the android asset while accessing html pages in android assets.
File gameDir = new File("/data/data/" + getActivity().getPackageName() + "/games");
gameDir.mkdirs();
// Create and execute the background task.
mTask = new DownloadTask();
mTask.execute("https://github.com/gabrielecirulli/2048/archive/master.zip", "/data/data/" + getActivity().getPackageName() + "/games/2048.zip");
mWebView.getSettings().setJavaScriptEnabled(true);
Toast.makeText(MainActivity.this, path+"/index.html", Toast.LENGTH_SHORT).show();
mWebView.loadUrl("file:///data/data/com.example.zipfiledownload/games/2048-master/index.html");
I'm new to Android development.
I want to load a html file into a webview.
Note that there are so many relevant questions on SO like this, but they all deal with getting **.html* from assets folder.
But I want to load html file from local folder, say "D://abc.html" because if my html is around 10Mb then corresponding apk size also goes upto 10mb.
Any help appreciated.
EDIT
I tried webView.loadUrl("file:///D:/Projects/myWebsite/index.html");
but it gives Web page not available and File not found error.
You can use:
WebView webView = // ...
webView.loadUrl("file:///myPath/myFile.html");
In an Android application, files can be read from 3 types of locations:
Internal storage: Each app has its own, file names are relative to this location. URL takes form file:///myFolder/myFile.html
External storage: Needs permission and may not always be available. Get the root folder by calling Environment.getExternalStorageDirectory(). So, construct the URL using: String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"
Assets: Stored in the apk. Read-only access. URL takes form file:///android_asset/myFolder/myFile.html (See also Loading an Android Resource into a WebView)
In Android 4.4 KitKat, a "Not allowed to load local resource: file:///.." is thrown.
arise when loadURL and the only alternative I've found is "loadDataWithBaseURL".
webView.loadDataWithBaseURL("file:///android_asset/demo/",
tmpDocumentText,"text/html", "UTF-8", null);
WebView has loadData method http://developer.android.com/reference/android/webkit/WebView.html
All you need to do is reading the file into String then feed it to WebView using loadData.
either one can use this way to load file specifically if the file is present within android studio project
(pls note android_res is res folder actually and should not be replaced)
webView.loadUrl("file:///android_res/drawable/FILENAME.EXTENSION");
but if that is not working please try another way of loading file like
webView.loadDataWithBaseURL("file:///android_asset/demo/",tmpDocumentText,
"text/html", "UTF-8", null);