Page 1 of 2
How to open local File on the device (PDF...)
Posted: Tue Jul 16, 2013 7:30 am
by dudi peretz
In order to ope local PDF file in iOS u need to do the following:
Open the "ChildBrowserViewController.m" file and edit the "loadURL" method by adding the following code:
(void)loadURL:(NSString*)url
{
NSLog(@"Opening Url : %@", url);
//Start - Added by dudi
if([url hasPrefix:@"file://"]) {
NSError *error = NULL;
Code: Select all
//Create the regular expression to match against
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"file://.*/Documents/" options:NSRegularExpressionCaseInsensitive error:&error];
// Create the new string by replacing the matching of the regex pattern with the template pattern(empty string)
NSString *relativeUri = [regex stringByReplacingMatchesInString:url options:0 range:NSMakeRange(0, [url length]) withTemplate:@""];
NSLog(@"New string: %@", relativeUri);
NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *url = [documentsDirectory URLByAppendingPathComponent:relativeUri];
NSLog(@"New string: %@", url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
//End - Added by dudi
else if ([url hasSuffix:@".png"]
[url hasSuffix:@".jpg"]
2.invoke the childBrowse plugin as following:
window.plugins.childBrowser.showWebPage("file:///mnt/myBill.pdf");
How to open local File on the device (PDF...)
Posted: Tue Jul 16, 2013 8:01 am
by Maryna Brodina
Hello! Thank you for posting that!
How to open local File on the device (PDF...)
Posted: Tue Jul 16, 2013 10:39 am
by dudi peretz
In order to ope local PDF file in Android u need to do the following:
open the ChildBrowser.java and do the following and edit the "execute" method:
if (action.equals("showWebPage")) {
...............
// start - load local file
String urlFile = args.getString(0);
if (urlFile.startsWith("file:")){
try{
this.openLocalFile(args.getString(0));
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
} catch (IOException e) {
return new PluginResult(PluginResult.Status.IO_EXCEPTION);
}
return new PluginResult(status, result);
}
// end - load local file
..................
this.browserCallbackId = callbackId;
open the ChildBrowser.java and do the following and add new "openLocalFile" method:
private void openLocalFile(String url) throws IOException {
// Create URI
Uri uri = Uri.parse(url);
Code: Select all
Intent intent = null;
// Check what kind of file you are trying to open, by comparing the url with extensions.
// When the if condition is matched, plugin sets the correct intent (mime) type,
// so Android knew what application to use to open the file
if (url.contains(".doc") || url.contains(".docx")) {
// Word document
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/msword");
} else if(url.contains(".pdf")) {
// PDF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
} else if(url.contains(".ppt") || url.contains(".pptx")) {
// Powerpoint file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
} else if(url.contains(".xls") || url.contains(".xlsx")) {
// Excel file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.ms-excel");
} else if(url.contains(".rtf")) {
// RTF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/rtf");
} else if(url.contains(".wav")) {
// WAV audio file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "audio/x-wav");
} else if(url.contains(".gif")) {
// GIF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/gif");
} else if(url.contains(".jpg") || url.contains(".jpeg")) {
// JPG file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpeg");
} else if(url.contains(".txt")) {
// Text file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "text/plain");
} else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) {
// Video files
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
}
//if you want you can also define the intent type for any other file
//additionally use else clause below, to manage other unknown extensions
//in this case, Android will show all applications installed on the device
//so you can choose which application to use
else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "*/*");
}
this.cordova.getActivity().startActivity(intent);
}
Then invoke the childBrowse plugin as following:
window.plugins.childBrowser.showWebPage("file:///mnt/myBill.pdf");
How to open local File on the device (PDF...)
Posted: Thu Sep 05, 2013 6:12 am
by Nathan Morin
Hi. Where do I find the ChildBrowser.java file?
How to open local File on the device (PDF...)
Posted: Thu Sep 05, 2013 3:26 pm
by maxkatz
Export the app as Eclipse project..then look for childBrowser plug-in.
How to open local File on the device (PDF...)
Posted: Thu Sep 05, 2013 4:10 pm
by Nathan Morin
Max-
Thanks for your reply. Forgive my ignorance, so I have exported the project as an Eclipse file. I have edited the file and saved it. What do I now do to make this an .apk package?
Also, everywhere I invoke (window.plugins.childBrowser.showWebPage("file:///mnt/myBill.pdf");) within appery.io will work once the Eclipse project is packaged as an apk?
Thanks for your help.
Cheers,
Nathan
How to open local File on the device (PDF...)
Posted: Thu Sep 05, 2013 4:18 pm
by maxkatz
You need to install the app on device.
When you launch the app via Test, the app is running in the browser, not in Appery.io
How to open local File on the device (PDF...)
Posted: Wed Sep 11, 2013 8:55 pm
by Deodorant King
So for all those that may be struggling with this. The ChildBrowser.java can be edited from within Appery.IO, one does NOT have to export it as Eclipse project.
It would have been helpful to have been told to do that in the first place rather than waste much time trying to get Eclipse to properly build an apk.
To find ChildBrowser.java: Source-Android-[Project Name]-src-com-phonegap-plugins-childbrowser
I am paying for Appery.io and it is disappointing that the easiest solution was not presented by the official rep.
How to open local File on the device (PDF...)
Posted: Wed Sep 11, 2013 8:58 pm
by maxkatz
It can, but if you make changes to .java file -- it has to be recompiled.
How to open local File on the device (PDF...)
Posted: Wed Sep 11, 2013 9:07 pm
by Deodorant King
I was able to make changes to the .java file, put in the correct invoke functions and export the project as .apk with no problems.