View Microsoft Word Document In Android - Example


In this Example we will see how to view a ms word document through an android app. Here we will open the .doc file saved in the SD card with an application which can view the doc file such as Quick Office , Office suite or any other application capable of reading .doc or docx files.

On tapping a button we check for the existence of the .doc file in the SD card. If the file exists we call the reader application via Intents. In the due course if the reader application is not found it is notified by catching the exception ActivityNotFoundException.


So lets start:

1.Create an Android Application Project with any Project Name and Package Name. Create an Activity and Layout file for the same like MainActivity & activity_main.

2.Design the screen for the activity with a button as below

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Vibrator">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp"
android:text="Welcome to AndroidBite !!!"
android:textColor="@android:color/black"
android:textSize="20sp"/>

<Button
android:id="@+id/bPressMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Press Me !!!"/>

</RelativeLayout>
3.Now in the activity, we create a File instance and construct it using specified directory and name. Then we set the Uri from the file instance and start the intent Intent.ACTION_VIEW . This intents data is set with the uri and the MIME Type for .doc file is set as application/msword before calling the intent.

For docx files use "application/vnd.openxmlformats-officedocument.wordprocessingml.document" as MIME type

MainActivity.java:
import java.io.File;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Button button = (Button) findViewById(R.id.bPressMe);

  button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {

    File file = new File(Environment.getExternalStorageDirectory(),
      "Help Desk Voice Flow.doc");

    try {
     if (file.exists()) {
      Uri path = Uri.fromFile(file);
      Intent objIntent = new Intent(Intent.ACTION_VIEW);
      // replace "application/msword" with
      // "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
      // for docx files
      // objIntent.setDataAndType(path,"application/msword");
      objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(objIntent);
     } else {
      Toast.makeText(MainActivity.this, "File NotFound",
        Toast.LENGTH_SHORT).show();
     }
    } catch (ActivityNotFoundException e) {
     Toast.makeText(MainActivity.this,
       "No Viewer Application Found", Toast.LENGTH_SHORT)
       .show();
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });

 }

}
4.Include the WRITE_EXTERNAL_STORAGE permission in the manifest file(permission for both reading and writing).
5.Run the application by right clicking the project Run As->Android Application. If everything goes well the output will be similar to the one below

Output: