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

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!

Related

Show pdf folder with its content to user

I have an app that save some pdf files in sdcard/android/data/com.myapp/files/pdf. I'd like to show its content to user, so he/she can open the pdfs, read it using another app, share...
This is my code to create the folder:
File folder = new File(Objects.requireNonNull(MainActivity.this
.getExternalFilesDir(null)).getAbsolutePath() + File.separator + "pdf");
SO, I'd like to open this folder to the user, because it is quite a hard task to open it all manually. Any ideas how can I do it in any android api version?

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

Can't get the correct music folder

I can't find out the correct folder to load my audio file.
I'm using an Android device which has no SD card but if I connect it via USB to my PC I will see the directory "Phone" with the subdirectory "Music".
In that directory I placed a mp3 file "Play.mp3" and then I tried the following variations to get a file reference:
File file = new File("/Phone/Music/Play.mp3");
File file = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC), "Play.mp3");
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "Play.mp3");
But in each case file.exists() returns false.

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
}

Unable to load local HTML file in Android WebView

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.

Categories

Resources