Load an external html file into webview - android

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

Related

Create VideoView from a video file that is inside of a Zip File - Android

I have successfully downloaded a .Zip file from my server, which is then stored on my tablet.
However, I now want to set VideoView.setUri() to one of the files in the .zip.
I have DownloadManager.getUriForDownloadedFile(referenceId) showing me that the zip's Uri is: content://downloads/my_downloads/305.
What I want to do now is take that Uri and feed it into the VideoView, without extracting it (if possible).
I have found that you can reference a zip's file via the extension:
"zip_path_zip_name" + "!/" + filename.ext".
So you could call:
VideoView.setVideoURI(Uri.parse("zipPathAndTitle" + "!/" + "test.mp4""));
But I'm stuck from the Uri of: content://downloads/my_downloads/305 getting that as a file I can reference, to then set as the VideoView's Uri.
Any help is greatly received.
First, extract the zip file to a specific folder in your storage. You can't use file inside this zip file unless you extract it. Get the path of the extracted file you want in this folder then set it to your VideoView.

load somefile.html file from internal storage to a webview of an app

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");

Loading html file from local folder into webview

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);

Accessing an app's project files on the Android platform

So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw in my eclipse project,
and com.alex.testing is my package name.
Still, when I try if(file.exists()) , the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
put the file in assets folder and pick the file from there
Now use Context.getAssets().open("jazz.pdf") and pass the resulting InputStream into PDf parser library
Ok, to access resources from current application you can use something like,
Uri path = Uri.parse("android.resource://<you package>/raw/<your_file.pdf>");
OR
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" R.raw.<your_file.pdf>);
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in /asset directory then copy it to any public access area then view that file from that path.
//if your are stored in SDcard your location would be
"data/data/com.alex.testing/res/raw/jazz.pdf"
//you read resources from raw folder thru the below code
InputStream inputStream = getResources().openRawResource(R.raw.jazz);
byte[] reader = new byte[inputStream.available()];

android: Check the existence of a file in the application

I have few html files in assets folder of my application. My application loads these files depending on the device language. When I check for the existance of the file it say does not exist, but when I load that file using browser.loadUrl(filename), it loads it fine.
Following code will help you to understand my problem:
String filename="file:///android_asset/actualfilemname.html";
File f = new File(filename);
if(!f.exist){
filename = "file:///android_asset/newfile.html";[Everytime it loads this file even though I have actualfilename.html in the folder]
}
browser.loadUrl(filename);
[it loads the newfile.html but not actualfilename.html]
You can't use File for resources. You'll need to use the AssetManager for that.
(In the off-chance that File does handle resources, which I don't think it does, you'll have to convert the path to a URI first, for example using URI.create(). File(String) expects a path, not a URI.)
Is this the exact code you are using? you probably want to be calling f.exists() not filename.exist().
Edit: try working with the AssetManager instead of hard coding your file path. My best guess is that the file path you are using is not exactly how it supposed to be.

Categories

Resources