Is there any possible way to open a .doc extension file?
-
1What do you mean by open the file. Do you want to view the file or what to use content of file. – MrWaqasAhmed Apr 20 '12 at 07:56
-
try Quick office in android market for open ms word,excel,pdf file viewer – Dinesh Apr 20 '12 at 08:01
-
3dude, I want to open with in my app activity – Narendra Apr 20 '12 at 08:03
-
http://stackoverflow.com/questions/2902689/how-can-i-read-a-text-file-from-the-sd-card-in-android this might help. – MrWaqasAhmed Apr 20 '12 at 08:04
6 Answers
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);

- 132,198
- 53
- 198
- 213
-
This worked for me, but I had to set the Target API to <= 23 otherwise I get an exception about the Uri being exposed outside the app. – Dan Harris Feb 24 '19 at 20:55
-
my word documents are always opend as "read-only", any suggestions ? I opend a question for this problem [here](https://stackoverflow.com/questions/56287019/word-document-is-always-read-only-when-opened-with-intent) – Aiko West May 24 '19 at 06:54
-
can't I open doc files inside my activity itself? like pdfView for pdf files.. – Sourav Kannantha B Apr 30 '21 at 08:46
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:"));
}

- 54,294
- 25
- 151
- 185
-
if (intent.resolveActivity(getPackageManager()) != null) { startActivity(Intent.createChooser(intent, "Choose an Application:")); } else { Toast.makeText(ctxt, "No suitable application installed.", Toast.LENGTH_SHORT).show(); } – ashishdhiman2007 Nov 14 '18 at 05:02
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);

- 5,303
- 1
- 31
- 33
-
So then, what is the `request_code` in `startActivityForResult()`. DOC doesn't represent a specific request code. – cagcak May 30 '14 at 02:07
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");

- 502
- 1
- 3
- 17
-
I was trying to see what options I had to create a file preview, didn't know you could send google docs an URL! – Jhuliano Moreno Jan 23 '15 at 20:45
-
1But the file is in local assets folder. You are describing a way to show remote files – Kannan_SJD Aug 08 '18 at 10:17
-
it showing widescreen can we fit inside mobile device, you have any solution for that – Kishan Donga Apr 29 '20 at 12:01
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.
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:
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.

- 1,615
- 2
- 22
- 25
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.

- 29,001
- 6
- 52
- 53