android - How to open a file using installed apps? -
i want open given file using installed apps in android. how can that?
i using following code google returns "no app open file"
. if open file in other file managers, see different options open file. code:
public void open(view v) { string name = ((textview) v).gettext().tostring(); file f = new file(path + name); if (f.isdirectory()) { showfiles(f); } else if (f.isfile()) { intent openfileintent = new intent(intent.action_view); openfileintent.setdata(uri.fromfile(f)); if (openfileintent.resolveactivity(getpackagemanager()) != null) { startactivity(openfileintent); } else { toast.maketext(this, "no app open file.", toast.length_short).show(); } } else { toast.maketext(this, "selected item not file.", toast.length_short).show(); } }
you should specify mime type android knows app capable of opening / viewing file. should work you:
public void open(view v) { string name = ((textview) v).gettext().tostring(); file f = new file(path + name); if (f.isdirectory()) { showfiles(f); } else if (f.isfile()) { intent openfileintent = new intent(intent.action_view); openfileintent.setdataandtype(uri.fromfile(f), getmimetypefromfile(f)); if (openfileintent.resolveactivity(getpackagemanager()) != null) { startactivity(openfileintent); } else { toast.maketext(this, "no app open file.", toast.length_short).show(); } } else { toast.maketext(this, "selected item not file.", toast.length_short).show(); } } private string getmimetypefromfile(file f){ uri fileuri = uri.fromfile(f); string fileextension = mimetypemap.getfileextensionfromurl(fileuri.tostring()); string filetype = mimetypemap.getsingleton().getmimetypefromextension(fileextension); if (filetype == null) filetype = "*/*"; return filetype; }
Comments
Post a Comment