Android 如何打开 .doc 扩展文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10242306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android how to open a .doc extension file?
提问by Narendra
Is there any possible way to open a .doc extension file?
有没有办法打开 .doc 扩展名文件?
回答by ρяσ?ρ?я K
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.
与 iOS 不同,Android 本身不支持渲染 .doc 或 .ppt 文件。您正在寻找一个公共意图,允许您的应用重复使用其他应用的活动来显示这些文档类型。但这仅适用于安装了支持此 Intent 的应用程序的手机。
http://developer.android.com/guide/topics/intents/intents-filters.html
http://developer.android.com/guide/topics/intents/intents-filters.html
or if you have installed some app then use this Intent:
或者如果您安装了一些应用程序,请使用此 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);
回答by Jared Burrows
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:"));
}
回答by sravan
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);
回答by Amar1989
you can open file in webview if you want to open within app. ex:
如果你想在应用程序中打开,你可以在 webview 中打开文件。前任:
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");
回答by Zulqarnain
Here is the complete way to open .doc file in Android 7.0 or less:
以下是在 Android 7.0 或更低版本中打开 .doc 文件的完整方法:
Step-1:First of all, place your pdf file in assets folder like the following screenshot.
步骤 1:首先,将您的 pdf 文件放在资产文件夹中,如下面的屏幕截图所示。
Step-2:Now go to build.gradle file and add following lines:
第 2 步:现在转到 build.gradle 文件并添加以下几行:
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.
第 3 步:现在添加一个新的 java 文件,它应该从FileProvider
Like 在我的案例中扩展,文件名是LegacyCompatFileProvider
其中的代码。
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:
第 4 步:"xml"
在"res"
文件
夹下创建一个名为的文件夹。(如果文件夹已经存在,则无需创建)。现在providers_path.xml
在xml
文件夹中添加一个文件。这是屏幕截图:
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:
第 5 步:现在转到AndroidManifest.xml
文件和<application></application>
标签中的以下几行:
<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:
第 6 步:现在转到要加载 pdf 的 Activity 类并添加以下 1 行和这 2 个方法:
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.
现在调用LoadPdfFile
方法并传递您的文件名,而.doc
不像我的情况"chapter-0"
,它将在 doc reader 应用程序中打开 doc 文件。
回答by jeet
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.
您可以将文件从原始资源复制到 sdcard,然后在 ACTION_VIEW 意图上调用 startActivity(),该意图具有指向可读副本的 Uri,并且还具有正确的 MIME 类型。
Of course, this will only work on a device that has a Word document viewer on it.
当然,这仅适用于装有 Word 文档查看器的设备。