android - How to open PDF or TXT file in default app on Xamarin Forms -


i going open document using default app in xamarin forms. tried approach doesn't work me , not sure reason.

device.openuri(new uri(file_path)); 

please give me great solution if knows how handle it. thanks.

you can use dependencyservice implement each platform. firstly, create interface pcl example like:

public interface ifileviewer {     void showpdftxtfromlocal(string filename); } 

then android platform, create class implement interface:

[assembly: xamarin.forms.dependency(typeof(fileviewer))]  namespace namespace.droid {     public class fileviewer : ifileviewer     {         public void showpdftxtfromlocal(string filename)         {             string dirpath = xamarin.forms.forms.context.getexternalfilesdir(android.os.environment.directorydocuments).path;             var file = new java.io.file(dirpath, system.io.path.combine(dirpath, filename));              if (file.exists())             {                 xamarin.forms.device.begininvokeonmainthread(() =>                 {                     var uri = android.net.uri.fromfile(file);                     intent intent = new intent(intent.actionview);                     var mimetype = mimetypemap.singleton.getmimetypefromextension(mimetypemap.getfileextensionfromurl((string)uri).tolower());                     intent.setdataandtype(uri, mimetype);                     intent.setflags(activityflags.clearwhentaskreset | activityflags.newtask);                      try                     {                         xamarin.forms.forms.context.startactivity(intent);                     }                     catch (exception ex)                     {                         system.diagnostics.debug.writeline(ex.tostring());                     }                 });             }             else             {                 system.diagnostics.debug.writeline("file not found");             }         }     } } 

this example works files placed in getexternalfilesdir(android.os.environment.directorydocuments), if files in other place, need modify code.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -