Narendra
Narendra

Reputation: 1868

Android how to open a .doc extension file?

Is there any possible way to open a .doc extension file?

Upvotes: 20

Views: 71037

Answers (6)

Zulqarnain Mustafa
Zulqarnain Mustafa

Reputation: 1653

Here is the complete way to open .doc file in Android 7.0 or less:

Step-1: First of all, place your pdf file in assets folder like the following screenshot. Placing doc file in assets folder

Step-2: Now go to build.gradle file and add following lines:

repositories {
maven {
    url "https://s3.amazonaws.com/repo.commonsware.com"
}

}

and then under dependencies add the following line and sync:

compile 'com.commonsware.cwac:provider:0.4.3'

Step-3: Now add a new java file which should extend from FileProvider Like in my case file name is LegacyCompatFileProvider and code inside of it.

import android.database.Cursor;
import android.net.Uri;

import android.support.v4.content.FileProvider;

import com.commonsware.cwac.provider.LegacyCompatCursorWrapper;

public class LegacyCompatFileProvider extends FileProvider {
  @Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    return(new LegacyCompatCursorWrapper(super.query(uri, projection, selection, selectionArgs, sortOrder)));
  }
}

Step-4: Create a folder named "xml" under "res" folder. (If the folder is already there then no need to create). Now add a providers_path.xml file, in xml folder. Here is the screenshot: Place of provider_path xml file

Inside file add following lines:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="stuff" />
</paths>

Step-5: Now go to AndroidManifest.xml file and following lines in <application></application> tag:

<provider
            android:name="LegacyCompatFileProvider"
            android:authorities="REPLACE_IT_WITH_PACKAGE_NAME"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

Step-6: Now go to the Activity class from where you want to load pdf and add following 1 line and these 2 methods:

private static final String AUTHORITY="REPLACE_IT_WITH_PACKAGE_NAME";

static private void copy(InputStream in, File dst) throws IOException {
        FileOutputStream out=new FileOutputStream(dst);
        byte[] buf=new byte[1024];
        int len;

        while ((len=in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
        out.close();
    }

    private  void LoadPdfFile(String fileName){

        File f = new File(getFilesDir(), fileName + ".doc");

        if (!f.exists()) {
            AssetManager assets=getAssets();

            try {
                copy(assets.open(fileName + ".doc"), f);
            }
            catch (IOException e) {
                Log.e("FileProvider", "Exception copying from assets", e);
            }
        }

        Intent i=
                new Intent(Intent.ACTION_VIEW,
                        FileProvider.getUriForFile(this, AUTHORITY, f));

        i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        startActivity(i);
        finish();
    }

Now call LoadPdfFile method and pass your file name without .doc like in my case "chapter-0" and it will open doc file in doc reader application.

Upvotes: 2

Jared Burrows
Jared Burrows

Reputation: 55527

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:"));
}

Upvotes: 14

Amar1989
Amar1989

Reputation: 512

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");

Upvotes: 4

sravan
sravan

Reputation: 5333

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);

Upvotes: 13

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

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);  

Upvotes: 32

jeet
jeet

Reputation: 29199

you can copy the file from the raw resource to sdcard, then call startActivity() on an ACTION_VIEW Intent that has a Uri pointing to the readable copy and also has the proper MIME type.

Of course, this will only work on a device that has a Word document viewer on it.

Upvotes: 0

Related Questions