0

I have files like .doc .pdf .excel... and i want to open them externally.

I tried intent.ACTION_VIEW but it open it in the browser.

How can I open them with which ever application the user has as a default handler?

File file = new File(Globals.SAVE_PATH + filename);
openFile(file.toURI());


public void openFile(URI uri) {
        Intent i = new Intent(?????);
        i.setData(Uri.parse(uri.toString()));
        startActivity(i);
    }
code511788465541441
  • 20,207
  • 61
  • 174
  • 298

2 Answers2

1

You might find this article useful. The thing you must remember is that if the phone has only one app that is capable of opening the file, it will launch automatically.

mtmurdock
  • 11,887
  • 20
  • 63
  • 102
0

Thanks for your answers but I found this method to be the easiest.

String path="File Path";

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);

MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=file.getName().substring(file.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);

intent.setDataAndType(Uri.fromFile(file),type);

startActivity(intent);
code511788465541441
  • 20,207
  • 61
  • 174
  • 298
  • This is nice since you dont have to know the mime type in advance, but you may want to change this to find the last index of ".". As you have it written now, any file that has '.' in the filename will break your code. – mtmurdock Jun 09 '12 at 20:51