{"id":36449,"date":"2024-11-26T14:08:07","date_gmt":"2024-11-26T06:08:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36449\/"},"modified":"2024-11-26T14:08:07","modified_gmt":"2024-11-26T06:08:07","slug":"android-%e6%89%93%e5%bc%80-word-%e6%96%87%e4%bb%b6%e6%8f%90%e7%a4%ba%e7%a8%8b%e5%ba%8f%e5%bc%82%e5%b8%b8%ef%bc%8c%e5%a6%82%e4%bd%95%e6%8e%92%e6%9f%a5%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36449\/","title":{"rendered":"Android \u6253\u5f00 Word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38\uff0c\u5982\u4f55\u6392\u67e5\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173115560255227.jpg\" class=\"aligncenter\" title=\"Android \u6253\u5f00 Word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38\uff0c\u5982\u4f55\u6392\u67e5\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"Android \u6253\u5f00 Word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38\uff0c\u5982\u4f55\u6392\u67e5\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5b89\u5353\u6253\u5f00 word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38<\/strong><\/p>\n<p>\u5f53\u4f7f\u7528 intent \u5c1d\u8bd5\u6253\u5f00 word \u6587\u4ef6\u65f6\uff0c\u7a0b\u5e8f\u5374\u81ea\u52a8\u5173\u95ed\uff0c\u63d0\u793a\u5f02\u5e38\u3002<\/p>\n<p><strong>\u95ee\u9898\u6392\u67e5\u53ca\u89e3\u51b3\u6b65\u9aa4<\/strong><\/p>\n<p><strong>1. \u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u6743\u9650<\/strong><\/p>\n<p>\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u5df2\u6388\u4e88\u8bfb\u53d6\u5916\u90e8\u5b58\u50a8\u7684\u6743\u9650\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728 androidmanifest.xml \u4e2d\u6dfb\u52a0\u5982\u4e0b\u6743\u9650\u5e76\u8bf7\u6c42\u8fd0\u884c\u65f6\u6743\u9650\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre>&lt;uses-permission android:name=\"android.permission.read_external_storage\" \/&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>if (contextcompat.checkselfpermission(this, manifest.permission.read_external_storage)\n        != packagemanager.permission_granted) {\n    activitycompat.requestpermissions(this, new string[]{manifest.permission.read_external_storage}, 1);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u6784\u5efa\u6253\u5f00 word \u6587\u4ef6\u7684 intent<\/strong><\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\u4e0d\u6b63\u786e\u6216 mime \u7c7b\u578b\u8bbe\u7f6e\u9519\u8bef\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6b63\u786e\u6784\u5efa\u6253\u5f00 word \u6587\u6863\u7684 intent\uff1a<\/p>\n<pre>public intent getwordfileintent(string filepath) {\n    file file = new file(filepath);\n    uri fileuri;\n    \n    if (build.version.sdk_int &gt;= build.version_codes.n) {\n        fileuri = fileprovider.geturiforfile(this, getpackagename() + \".fileprovider\", file);\n    } else {\n        fileuri = uri.fromfile(file);\n    }\n    \n    intent intent = new intent(intent.action_view);\n    intent.setdataandtype(fileuri, \"application\/msword\");\n    intent.addflags(intent.flag_grant_read_uri_permission);\n    return intent;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u8bbe\u7f6e fileprovider<\/strong><\/p>\n<p>\u5bf9\u4e8e android 7.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff0c\u9700\u8981\u4f7f\u7528 fileprovider \u5b89\u5168\u5730\u5171\u4eab\u6587\u4ef6 uri\u3002\u5728 androidmanifest.xml \u4e2d\u6dfb\u52a0\uff1a<\/p>\n<pre>&lt;provider\n    android:name=\"androidx.core.content.fileprovider\"\n    android:authorities=\"${applicationid}.fileprovider\"\n    android:exported=\"false\"\n    android:granturipermissions=\"true\"&gt;\n    &lt;meta-data\n        android:name=\"android.support.file_provider_paths\"\n        android:resource=\"@xml\/file_paths\" \/&gt;\n&lt;\/provider&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u521b\u5efa res\/xml\/file_paths.xml \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;paths xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\n    &lt;external-path name=\"external_files\" path=\".\" \/&gt;\n&lt;\/paths&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p>\u5c06\u542f\u52a8\u6d3b\u52a8\u7684\u4ee3\u7801\u653e\u5165 try-catch \u8bed\u53e5\u4e2d\uff0c\u4ee5\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\u5e76\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<pre>try {\n    Intent intent = getWordFileIntent(path);\n    startActivity(intent);\n} catch (ActivityNotFoundException e) {\n    Toast.makeText(this, \"\u627e\u4e0d\u5230\u53ef\u4ee5\u6253\u5f00\u6b64\u6587\u4ef6\u7684\u5e94\u7528\", Toast.LENGTH_LONG).show();\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fAndroid \u6253\u5f00 Word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38\uff0c\u5982\u4f55\u6392\u67e5\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u5353\u6253\u5f00 word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38 \u5f53\u4f7f\u7528 intent \u5c1d\u8bd5\u6253\u5f00 word \u6587\u4ef6\u65f6\uff0c\u7a0b\u5e8f\u5374\u81ea\u52a8\u5173\u95ed\uff0c\u63d0\u793a\u5f02\u5e38\u3002 \u95ee\u9898\u6392\u67e5\u53ca\u89e3\u51b3\u6b65\u9aa4 1. \u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u6743\u9650 \u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u5df2\u6388\u4e88\u8bfb\u53d6\u5916\u90e8\u5b58\u50a8\u7684\u6743\u9650\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728 androidmanifest.xml \u4e2d\u6dfb\u52a0\u5982\u4e0b\u6743\u9650\u5e76\u8bf7\u6c42\u8fd0\u884c\u65f6\u6743\u9650\u6765\u5b9e\u73b0\uff1a &lt;uses-permission android:name=&#8221;android.permission.read_external_storage&#8221; \/&gt; \u767b\u5f55\u540e\u590d\u5236 if (contextcompat.checkselfpermission(this, manifest.permission.read_external_storage) != packagemanager.permission_granted) { activitycompat.requestpermissions(this, new string[]{manifest.permission.read_external_storage}, 1); } \u767b\u5f55\u540e\u590d\u5236 2. \u6784\u5efa\u6253\u5f00 word \u6587\u4ef6\u7684 intent \u4f7f\u7528\u65b9\u6cd5\u4e0d\u6b63\u786e\u6216 mime \u7c7b\u578b\u8bbe\u7f6e\u9519\u8bef\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6b63\u786e\u6784\u5efa\u6253\u5f00 word \u6587\u6863\u7684 intent\uff1a public intent getwordfileintent(string filepath) { file file = new file(filepath); uri fileuri; if (build.version.sdk_int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36449","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36449"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36449\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}