Android how to open a .doc extension file?

Solution 1:

Unlike iOS, Android itself does not support rendering .doc or .ppt files. You are looking for a public intent that allows your app to reuse other apps' activities to display these document types. But this will only work for a phone that has an app installed that supports this Intent.

http://developer.android.com/guide/topics/intents/intents-filters.html

or if you have installed some app then use this Intent:

//Uri uri = Uri.parse("file://"+file.getAbsolutePath());
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = "application/msword";
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);  

Solution 2:

Here is a method to take care of this for you:

public void openDocument(String name) {
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File(name);
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    if (extension.equalsIgnoreCase("") || mimetype == null) {
        // if there is no extension or there is no definite mimetype, still try to open the file
        intent.setDataAndType(Uri.fromFile(file), "text/*");
    } else {
        intent.setDataAndType(Uri.fromFile(file), mimetype);            
    }
    // custom message for the intent
    startActivity(Intent.createChooser(intent, "Choose an Application:"));
}

Solution 3:

Open Document from List of avaliable application User have to choose application from list of application

File targetFile = new File(path);
                    Uri targetUri = Uri.fromFile(targetFile);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(targetUri, "application/*");
                    startActivityForResult(intent, DOC);

Solution 4:

you can open file in webview if you want to open within app. ex:

 String doc="<iframe src='http://docs.google.com/viewer?    url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true'"+
    " width='100%' height='100%' style='border: none;'></iframe>";

        WebView  wv = (WebView)findViewById(R.id.fileWebView); 
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setAllowFileAccess(true);
        //wv.loadUrl(doc);
        wv.loadData( doc , "text/html",  "UTF-8");