dudi peretz
Posts: 0
Joined: Wed Jun 26, 2013 8:15 am

How to open local File on the device (PDF...)

In order to ope local PDF file in iOS u need to do the following:

  1. Open the "ChildBrowserViewController.m" file and edit the "loadURL" method by adding the following code:

  2. (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");

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How to open local File on the device (PDF...)

Hello! Thank you for posting that!

dudi peretz
Posts: 0
Joined: Wed Jun 26, 2013 8:15 am

How to open local File on the device (PDF...)

In order to ope local PDF file in Android u need to do the following:

  1. 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;

  2. 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");

Nathan Morin
Posts: 0
Joined: Thu Sep 05, 2013 6:12 am

How to open local File on the device (PDF...)

Hi. Where do I find the ChildBrowser.java file?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How to open local File on the device (PDF...)

Export the app as Eclipse project..then look for childBrowser plug-in.

Nathan Morin
Posts: 0
Joined: Thu Sep 05, 2013 6:12 am

How to open local File on the device (PDF...)

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

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How to open local File on the device (PDF...)

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

Deodorant King
Posts: 0
Joined: Fri Aug 30, 2013 5:48 pm

How to open local File on the device (PDF...)

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.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How to open local File on the device (PDF...)

It can, but if you make changes to .java file -- it has to be recompiled.

Deodorant King
Posts: 0
Joined: Fri Aug 30, 2013 5:48 pm

How to open local File on the device (PDF...)

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.

Return to “Issues”