Unable to load local HTML file in Android WebView - java

I have a WebView. I want to load a local HTML file called helloworld.html. It is located in my drawable-hdpi folder.
Here is my code:
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/helloworld.html");
I get a browser error:
The webpage at file:///android_asset/helloworld.html could not be loaded as the file requested was not found. /android_asset/helloworld.html (no such file or directory)
How can I resolve?

Put your HTML file in the assets folder.

Related

How to show html file from specific folder in webView on Android

In storage, I have one folder and this folder name is: my_app
In this folder I have one HTML file and i want open this HTML file into webView!
I write below codes, but after run application show me this error on webView: Web page not available and not open my HTML file!
My codes:
htmlReaderPage_reader.getSettings().setJavaScriptEnabled(true);
htmlReaderPage_reader.setInitialScale(1);
htmlReaderPage_reader.getSettings().setLoadWithOverviewMode(true);
htmlReaderPage_reader.getSettings().setUseWideViewPort(true);
htmlReaderPage_reader.getSettings().setBuiltInZoomControls(false);
htmlReaderPage_reader.getSettings().setPluginState(WebSettings.PluginState.ON);
htmlReaderPage_reader.getSettings().setDomStorageEnabled(true);
htmlReaderPage_reader.getSettings().setDatabaseEnabled(true);
htmlReaderPage_reader.setWebViewClient(new WebViewClient());
htmlReaderPage_reader.setVerticalScrollBarEnabled(true);
htmlReaderPage_reader.setHorizontalScrollBarEnabled(true);
For open from storage I write below codes:
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/my_app/index.html");
String uri = Uri.fromFile(new File(file.getAbsolutePath())).toString();
htmlReaderPage_reader.loadUrl(uri);
But when open this HTML file from assets folder, open this file and not have any error!
For open from assets I write below codes:
htmlReaderPage_reader.loadUrl("file:///android_asset/my_app/index.html");
htmlReaderPage_reader.loadUrl(uri);
Why open this file from assets but not open from storage!
I should open this from storage and a specific folder!

Opening pdf from assets

I'm currently trying to use dynamic delivery to download and install some pdf files and open them with barteksc's pdf viewer (com.github.barteksc:android-pdf-viewer:2.8.2).
When I had the pdf files inside my apk everything worked fine but now that I download them and install them via dynamic delivery I get this error when I try to open them
E/PDFView: load pdf error
java.io.FileNotFoundException: Example.pdf
at android.content.res.AssetManager.nativeOpenAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:744)
at android.content.res.AssetManager.open(AssetManager.java:721)
at com.github.barteksc.pdfviewer.util.FileUtils.fileFromAsset(FileUtils.java:37)
at com.github.barteksc.pdfviewer.source.AssetSource.createDocument(AssetSource.java:39)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
This returns "Installed"
if (manager.getInstalledModules().contains("dynamicfeature")) {
statusText.setText("Installed");
} else {
statusText.setText("Feature not yet installed");
}
I am installing the module from a different Class if that makes a difference, I saw something about updating the context in Google docs but I could not figure it out.
I have also added
com.google.android.play.core.splitcompat.SplitCompatApplication
as the documentation says, still no luck.
This is the method I am using to view the Pdf file.It was working perfect while the pdfs where in the asset folder of the base application but it's not working now that the pdfs are in the assets folder of the Dynamic Delivery Module.
private void displayFromAsset(String assetFileName) {
String SAMPLE_FILE = getIntent().getStringExtra("PDF INTENT");
pdfFileName = assetFileName;
pdfView.fromAsset(SAMPLE_FILE)
.defaultPage(pageNumber)
.enableSwipe(true)
.swipeHorizontal(false)
.onPageChange(this)
.enableAnnotationRendering(true)
.onLoad(this)
.scrollHandle(new DefaultScrollHandle(this))
.load();
Example.pdf is not pointing to the asset folder.
you need to provide a relative patht of your file .
for asset folder u can giv like
Uri resUri= Uri.fromFile(new File("//android_asset/image.png"));
For downloaded pdf files you can query by contentprovider query & get the uri from query result.

where to store file in android project

I have a file with some data that I want only my application to have access to.
I was wondering in which folder I should put the file in my android project in order to have access to it using
File file = new File(context.getFilesDir(), filename);
Thanks
This is just an empty folder in Android device.
To access your files in assets
InputStream is = getAssets().open("subfolder/anyfile.txt");
to access files inside raw use
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.somesrc);
getFilesDir() is just a folder that uses for openFileOutput(String, int) method. More detailed: https://stackoverflow.com/a/21230946/1979882
Use Assets Folder For files that will be bundled with the application and will be stored inside the apk as is. you can add an "asset" folder to your project by right clicking your app in the project explorer then select
New=> Folder=> Assets Folder.
you can open the files stored in the assets folder using the assets manager for example to open an image kept in /assets/img/image1.png:
InputStream is = assetManager.open("img/image1.png");
for HTML files you can load the file by its URL :
webView.loadUrl("file:///android_asset/"+name);
Here you can get a clear functionality of saving,retrieving a file
Here is the Link:http://www.mysamplecode.com/2012/06/android-internal-external-storage.html

How to get path to res.drawable folder to copy a file?

I am writing my app in AndroidStudio, I got gif file in my drawable/gifs folder and I wish to copy that file to MediaStore.Images.Media folder after clicking a button.
Currently I can't get my gif path even when using some answers posted.
The path I've tried using is
android.resource://com.example.bartoszwolski.cluainkeyboard/drawable/gifs/my.gif
Just use Uri:
Uri fileUri = Uri.parse("android.resource://your_packagename/" + R.drawable.your_image_id);
and:
new File(fileUri.getPath());

I want to supply some pdf documents with my application

After certain research and lookup over the internet I found that I cannot place these files in the assets folders as those files will be private to my application, I want the native pdf application to be invoked on a button press which redirects the user to the particular document. What I am confused about is how do I bundle the files with my application so that when the user downloads or installs my application these files get stored in her SD-Card.
copy your assests files to the sdcard and when clicking on the button open the files in the sdcard as like this
File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/filepath/"+fileName);//give the file path here
if(file.exists())
{
// do action here
}

Categories

Resources