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
Related
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
I wrote a script that allows you to read and write to a json file. As long as I tried the script in unity editor, I didn't have any problems. But when I switched to installing the application on an Android device, I can't find my json file.
The json file is called "playerData.json" and has been placed in a folder called "playerData.json".
This is the code for the path of the json file (working on unity but not working on android)
path = Application.streamingAssetsPath + "/playerData.json";
I tried using this string of code to get the code to work on android as well, but there is no way to get it to work.
path = System.IO.Path.Combine(Application.streamingAssetsPath, "/playerData.json");
I only wrote the line concerning the path as my android device cannot find the JSON file. Thanks everyone for the help!
Application.streamingAssetsPath does not work on Android and WebGL
It is not possible to access the StreamingAssets folder on WebGL and Android platforms. No file access is available on WebGL. Android uses a compressed .apk file. These platforms return a URL. Use the UnityWebRequest class to access the Assets.
There is a workaround though as mentioned using UnityWebRequest, can find an example in this Unity Answer
I am working with Cordova to build a hybrid app in webview.
All my links work fine as long as I switch from one html page to another.
But now I am trying to get android to present the user with a dialog to choose how to open a .pdf file (residing in the res folder of www -local file that comes with the app) with no success.
Here are my latest efforts (none of them does anything -at all)
<span>my test 01</span>
<a window.open("../res/DOC.pdf", "_system"); >my test 02</a>
my test 03
What am I doing wrong?
Thanks
I am coding a app for iOS and Android using Phonegap (version: 2.7.0). The pdf file is file:///android_asset/www/test.pdf (compressed in the .apk).
I try to open this pdf with a native pdf viewer like:
window.open('file:///android_asset/www/test.pdf', '_system', 'location=yes');
The command above works on browsers and iOS (you have to use _blank instead of _system)
I tried to load the file with the Phonegap File API to the sdcard, but I read its not possible to access files in the android_asset/www.
Is it possible to open the pdf directly from the android_asset/www path?
Is it possible to copy the file to sdcard end open it from there?
Maybe its possible with android install scripts?
Edit:
I added this to my .java file in the /src/com/...etc .java File:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
super.appView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
But there is still the same problem...
I have output from adb -d logcat it happens when i start the app and click the pdf link:
I/ActivityManager(22040): Displayed com.company.appname/.Appname: +1s611ms
I/ActivityManager(22040): Displayed com.tf.thinkdroid.samsung/com.tf.thinkdroid.pdf.app.PdfRenderScreen: +1s549ms
D/CordovaLog(27549): exception firing pause event from native
I/Web Console(27549): exception firing pause event from native at undefined:1
...
I/InputReader(22040): dispatchTouch::touch event's action is 0
I/InputDispatcher(22040): Delivering touch to current input target: action: 0, channel '40a9f450 com.tf.thinkdroid.samsung/com.tf.thinkdroid.pdf.app.PdfRenderScreen (server)'
I/InputReader(22040): dispatchTouch::touch event's action is 1
I/InputDispatcher(22040): Delivering touch to current input target: action: 1, channel '40a9f450 com.tf.thinkdroid.samsung/com.tf.thinkdroid.pdf.app.PdfRenderScreen (server)'
....
D/DroidGap(27549): Resuming the App
I believe this might work in your case:
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
Originally answered here: Allow opening of local files in HTML5 Phonegapped Android app
I had the same problem as the OP and many unsuccessful approaches later I came to the following solution (tested with Cordova 3.1.0 & Samsung Galaxy S3):
Solution
First, get the file by requesting the file system and then download it from your server with fileTransfer.download(). After completing the download call the FileOpener (which will open a pop up where to choose from apps like Adobe Reader). Note: Make sure to include the FileTransfer feature via CLI/Terminal: cordova plugin add org.apache.cordova.file
Navigate to your project folder
Open CLI/Terminal and type in: cordova plugin add https://github.com/don/FileOpener.git
After successful download and storage of your file on the device (see above) you open it with: window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc")
That's it! Good luck!
I have developed an application,but when my client tries to download the application from the url provided using Samsung GT19003,it is downloading as .zip file and on click of that it says could not open the file error.Whereas it downloads as .apk and works good in our devices.Please help me as how can i resolve the issue.
Make sure that you have set the http Content-Type to application/vnd.android.package-archive
This let the chrome know that it is an application not a zip.
A simple solution is to add the below line in /etc/mime.types
application/vnd.android.package-archive apk
And restart your apache.
This will force browsers to download files as "apk" and not append "zip" to it.
if its being downloaded as .zip
You or your client can use File Expert application to unzip this .zip file it must show .apk file in the unzip folder
give a direct apk link to your user either he/she use default browser to download or use some other like opera and if it is .apk file link then it must be downloaded as is.
you may be using some content site which zip file on particular download methods please ensure it for direct .apk link.
EDIT 1:
Last but not the least .apk is just a zip file so this is not an issue just rename it(From .Zip to .apk) if you could and i hope it will solve the problem
In my case also the issue was same as #alireza-fattahi mentioned regarding the header.
Sharing this here because in our case the file was in Azure Storage and accessed by a CDN in case anyone has the same issue.
Fix for Azure Sorage and CDN
By default the APK blob that was uploaded had content type of application/zip and the same content type reflected in the reposnse header causing the downloaded file to get a .zip extension on Android OS when download was finished.
I had to set a custom rule in the storage CDN to overwrite content-type header in the response to application/vnd.android.package-archive
Remember to purge the CDN cache after you set the new rule
Read more about Azure CDN rules engine.
Here is the Example :
// set your download apk path here
$path= public_path(). "/upload/apk/Ninja11.apk";
//Set header here like this
$headers = [
'Content-Type'=>'application/vnd.android.package-archive',
'Content-Disposition'=> 'attachment; filename="ninja-release.apk"',
];
return response()->file($path , $headers);
This is Laravel framework code but in the same way, you can use in other frameworks. Simply you need to set the Content type
Note : This is not Android side issues