I want to read a local html file to open it in Android WebView.
Why does the following work:
webViewMain.apply {
loadUrl("file:///android_asset/index.html")
}
But this code
webViewMain.loadDataWithBaseURL("file:///android_asset/",
URL("file:///android_asset/index.html").readText(Charset.forName("UTF-8")),
"text/html",
"UTF-8",
null)
gives me the following error: java.io.FileNotFoundException: /android_asset/index.html
The android file should be in the assets folder which is part of the root directory of the project.
assets/index.html
If you see the structure in the explorer it will be
/app/src/main/assets/index.html
and call webViewMain.loadUrl("file:///android_asset/index.html");
Things to check:
Make sure your assets folder spelling is right
The file is in the correct directory since it's giving you a FileNotFoundException
Related
I am making an app which use the following java line:
Net dnnNet = Dnn.readNetFromONNX("path\to\file");
As you can see, readNetFromONNX requires path to onnx file.
So I put my onnx file under assest folder, so I can use it at run time.
The problem is I you can only read assets foler with AssestManager and inputstream... the readNetFromONNX needs path...
How do I overcome this probelm? Is there a way to get the path of the file at run time? Maybe other folder?
Thanks from advance
I am creating an app in android studio that uses library opencv and yolo. I want to store the yolo config file and weights inside the android package. Right now I have those file in the external storage of my phone and I access them like this:
String yoloCfg = Environment.getExternalStorageDirectory() + "/dnns/traffic-yolov3-tiny.cfg" ;
String yoloWeights = Environment.getExternalStorageDirectory() + "/dnns/traffic-yolov3-tiny_15000.weights";
yolo = Dnn.readNetFromDarknet(yoloCfg, yoloWeights);
My question is: where do I put yolo config file and weights for them to be inside the app when the user downloads the apk and how do I get the path to them, since readNetFromDarknet needs as args the path of those files.
I have tried put them in the asset and attempt to get the path to asset but it doesnt work. This is the code I tried:
String yoloCfg = "file:///android_asset/traffic-yolov3-tiny.cfg";
String yoloWeights = "file:///android_asset/traffic-yolov3-tiny_15000.weights";
yolo = Dnn.readNetFromDarknet(yoloCfg, yoloWeights);
This is where I have the files:
Location of files
And this is the error I get:
E/cv::error(): OpenCV(4.0.1) Error: Parsing error (Failed to parse NetParameter file: file:///android_asset/traffic-yolov3-tiny.cfg) in readNetFromDarknet, file /build/master_pack-android/opencv/modules/dnn/src/darknet/darknet_importer.cpp, line 207
E/org.opencv.dnn: dnn::readNetFromDarknet_10() caught cv::Exception: OpenCV(4.0.1) /build/master_pack-android/opencv/modules/dnn/src/darknet/darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: file:///android_asset/traffic-yolov3-tiny.cfg in function 'readNetFromDarknet'
E/SurfaceView: Exception configuring surface
I had the exact same issue. What I did to fix this was to add the files in the asset folder; then I created two files and copied the content of the asset folder files there.
Make sure to use the following path and create your files there:
applicationContext.filesDir.absolutePath
I want to load a local web content on a webview. I'm downloading a zip file, unzipping it and saving the files (main.html and resource files - css, js, fonts, pngs, etc) on internal storage (/data/data/<app>/files/).
All files are in the same directory:
|- main.html
|- file.js
|- ...
So, the html file points resource files as follows:
<script type="text/javascript" src="file.js"></script>
I though this would work
String path = context.getFilesDir() + File.separator + "main.html";
webview.loadUrl("file://" + path)
but the webview shows file not found error in pre lollipop devices, and in lollipop devices shows:
"The webpage at file:///data/data/<app>/files/main.html could not be loaded because: net::ERR_ACCESS_DENIED"
If I load the html as a string by running
String file = htmlFile.fileToString();
webview.loadDataWithBaseURL("", file, "text/html", "utf-8", "");
it works, but I have to resolve the dependencies by providing the full path of all resource files. There must be a better way to deal with this.
Does anyone know how to load a local file on a webview that was stored at runtime in Android?
Thanks
I forgot the config methods of the webview.
webview.getSettings().setAllowFileAccess(false)
This was the problem. By turning to true the webview is able to load local files.
I need to open a pdf in android mobile device.
For that ,I have add a 'test.pdf' pdf file in common folder and add plugin (Childbrowser ) in config.xml file (res/xml/config.xml). And again add childbrowser.js and cordova.js file in js folder .
And add a code to open a pdf file here---
window.open('./test.pdf','_system','location=yes');
But still exception is coming like that 'Target file is not available' andgive a path
file.///data/data/
I want to load that test.pdf file in the android mobile and open that particular file .
But it is not opening .
Please suggest me a solution. Thanks
Unfortunately, Android does not support viewing PDFs out of the box in a WebView. Most results point to using Google Docs.
Here are some suggestions:
Open PDF in a WebView
How to open local pdf file in webview in android?
http://developer.appcelerator.com/question/147570/open-remote-pdf-file-on-webview-in-android
This you could do by combining a Cordova plug-in and this native code: http://kylewbanks.com/blog/Loading-PDF-in-Android-WebView
http://asmncl.blogspot.co.il/2012/06/android-open-pdf-file-in-webview.html
I'm trying to open a file with this:
document = builder.parse(new File("Data.xml"));
and I'm getting this message:
/Data.xml: open failed: ENOENT (No such file or directory)
and the file is in the root directory of the android project.
You are trying to open a file located in / (in linux this is the root directory of your file system). Instead you should be trying to create a file either on the SDCard or within the local storage directory for your application.
See this for more clarification: http://developer.android.com/guide/topics/data/data-storage.html
Move Data.xml into the assets folder of your project. Then to get a file reference, call getResources().getAssets().openFd( "Data.xml" )
You should probably try using a file input stream constructor for the builder instead, and use openFileInput( String fileName ) to get that, which does only use your app's data directory.
Using persistent storage
openFileInput()