{"id":203609,"date":"2025-05-22T11:03:57","date_gmt":"2025-05-22T03:03:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203609\/"},"modified":"2025-05-22T11:03:57","modified_gmt":"2025-05-22T03:03:57","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:\/\/server.hk\/cnblog\/203609\/","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><b><\/b>     <\/p>\n<h1>Android \u6253\u5f00 Word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38\uff0c\u5982\u4f55\u6392\u67e5\u89e3\u51b3\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aAndroid \u6253\u5f00 Word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38\uff0c\u5982\u4f55\u6392\u67e5\u89e3\u51b3\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241113\/1731479093673446354f9e6.jpg\" class=\"aligncenter\"><\/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<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><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><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>\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><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>\u4ee5\u4e0a\u5c31\u662f\u300aAndroid \u6253\u5f00 Word \u6587\u4ef6\u63d0\u793a\u7a0b\u5e8f\u5f02\u5e38\uff0c\u5982\u4f55\u6392\u67e5\u89e3\u51b3\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android \u6253\u5f00 Word &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203609","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203609"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203609\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}