Open pdf in .apk with native android application using phonegap - android

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!

Related

Cordova: cordova_plugins.js failed to load resource

I am learning to use cordova and i have been just doing a normal install of android platform in my cordova application, everything worked fine except for some errors i don't intent to fix just yet (which are external resource files loaded from javascript). I just installed phonegap-plugin-push for working on a push notification support, i believe i have to first register the device on which the notification should be recieved so i ran the app without any other configuration rather than downloading the libraries and putting the google-services.json file at the root application folder.
I try to find the commands for pushing but i am unabled to do so and i think it might be because of an error thrown in the console
Failed to load resource: net::ERR_FILE_NOT_FOUND /cordova_plugins.js
so i have been searching on google and i have found that i don't need to add anything but cordova.js as i have already done upon cordova application installation in order to work with cordova features.
I don't know if i have to add something else or if i am missing some code in order to make this plugin work, probably this plugin is not compatible with cordova but phonegap is built over cordova so i don't know.
in case it is necessary, this are the versions i am using:
Cordova-CLI: 7.1.0
Google Play Services: 46
Android support repository: 47
Android SDK: 26
I failed to say that the application running in cordova is done with angular and angular-ui-router.
After long hours of testing i've found out that the problem is caused by angular-ui-router, because i was using angular-ui-router i have set a <base href> tag in the head tag which is what is making cordova to fail, this is now actually how cordova is intended to work, it is because of how the files are placed. On android the files are placed in android_asset/www/, i don't know how the files are placed in other platforms, but when <base href> tag is set with a new value then the calls are now trying to be done in a directory where the files are not placed.
The solution in angular is to not set a <base href> tag and disable html5Mode in the config
app.config(["$stateProvider", "$locationProvider", "$urlRouterProvider",
function($stateProvider, $locationProvider, $urlRouterProvider){
// Set the default url state
// NOTE: Do not set <base href> tag, this breaks cordova calls
$urlRouterProvider.otherwise("/");
// Disable html5 native router mode
$locationProvider.html5Mode(false);
......
}
]);
I've realized this because in chrome device remote inspector i was getting error calls to wrong directories, by looking at the url where the application is running i saw that the calls were not in the proper directory

cannot use cordova plugin to open files under ionic 2 and typescript

I am developing an android app based on ionic 2 using typescript.
I would like o open a PDF file inside my app with another app that is registered for the fyletype (e.g. Acrobat Reader).
Therefore i tried the two standard plugins:
https://github.com/disusered/cordova-open
https://github.com/pwlin/cordova-plugin-file-opener2
Although ive added both plugins via "ionic plugin add ..." and of course played around with several combination referencing to it
the result is always that they ere not found
cordova-open
var open = cordova.plugins.disusered.open;
Property 'disusered' does not exist on type 'CordovaPlugins'.
cordova-plugin-file-opener2
cordova.plugins.fileOpener2.open(
filePath,
fileMIMEType,
{
error : function(){ },
success : function(){ }
}
);
Property 'fileOpener2' does not exist on type 'CordovaPlugins'.
I am running the app on an emulator via ionic run android
Could you please give some hint how i can achieve to use one of these plugins?
Thank you very much
Shane

android cordova app: opening .pdf file stored in www

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

Open pdf in Android Mobile using IBM Worklight studio

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

Camera with PhoneGap and Sencha touch

I'm trying to acccess the Camera using Sencha touch, ported to Android using Phonegap. As the Sencha touch guide to using native APIs suggests, I declared the camera as a required:
Ext.require('Ext.device.Camera');
But unfortunately, I get this error:
07-19 11:51:42.159: E/Web Console(8668): Uncaught Error: [Ext.Loader]
Failed loading './device/Camera.js', please verify that the file exists at
file:///android_asset/www/lib/touch/sencha-touch-all-debug.js:8000
The sencha library does have Camera.js under device. I'm not sure why this error appears.
Edit 1: Still not able to find an answer. Would really appreciate any suggestions.
If you download the source code of sencha touch, there will be a directory named "device" in the "src" directory (after unzipping). Camera.js is in that directory. Seems sencha-touch-all-debug.js or sencha-touch-all.js not include the source code from all .js files in "device" directory. You must manually copy that directory to your android assets directory (eg the file:///android_asset/www/lib/touch/device).
or
You can copy the directory device in any place on your android assets (eg file:///android_asset/www/device). Then on the first of javascript declaration before you use sencha touch you must type the following code:
Ext.Loader.setConfig ({
paths: {
'Ext.device': 'device'
}
});
The code tells that "Ext.device" is reference to directory "device". Make sure the "device" directory is relative to the html file that calls the javascript (eg index.html). From the above code means the "device" directory is same location with the html file.
If you are using phonegap for deploying, then use Phonegap camera functionality. I have used it in my app in following way:
Add <script type="text/javascript"
src="cordova-x.x.x.js"></script> in your index.html file.
Use following code to open Camera:
navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] );
For more information on this api use following link:
http://docs.phonegap.com/en/edge/cordova_camera_camera.md.html#Camera
On the sencha forum there is a thread about it.
The problem appears to be the naming of Phongap - Cordova. Executing this before Sencha Touch is loaded should fix that.
if(typeof window.Cordova != 'undefined') {
window.PhoneGap = window.Cordova;
}

Categories

Resources