{"id":36530,"date":"2024-11-26T17:03:34","date_gmt":"2024-11-26T09:03:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36530\/"},"modified":"2024-11-26T17:03:34","modified_gmt":"2024-11-26T09:03:34","slug":"%e5%ae%89%e5%8d%93%e6%89%93%e5%bc%80-word-%e6%96%87%e6%a1%a3%e6%97%b6%e5%87%ba%e9%94%99%ef%bc%8c%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e5%90%af%e5%8a%a8-activity-%e5%a4%b1%e8%b4%a5%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36530\/","title":{"rendered":"\u5b89\u5353\u6253\u5f00 Word \u6587\u6863\u65f6\u51fa\u9519\uff0c\u5982\u4f55\u89e3\u51b3\u542f\u52a8 Activity \u5931\u8d25\u95ee\u9898\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173131956449354.jpg\" class=\"aligncenter\" title=\"\u5b89\u5353\u6253\u5f00 Word \u6587\u6863\u65f6\u51fa\u9519\uff0c\u5982\u4f55\u89e3\u51b3\u542f\u52a8 Activity \u5931\u8d25\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"\u5b89\u5353\u6253\u5f00 Word \u6587\u6863\u65f6\u51fa\u9519\uff0c\u5982\u4f55\u89e3\u51b3\u542f\u52a8 Activity \u5931\u8d25\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5b89\u5353\u6253\u5f00 word \u6587\u6863\u65f6\u51fa\u9519<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n<p>\u6267\u884c startactivity(intent); \u540e\uff0c\u7a0b\u5e8f\u81ea\u52a8\u5173\u95ed\u3002\u6587\u4ef6\u8def\u5f84\u4e3a &#8220;\/storage\/emulated\/0\/\u68c0\u6d4b\u62a5\u544a\/20240919151923_\u68c0\u6d4b\u8bb0\u5f55.docx&#8221;\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p><strong>1. \u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u7684\u6743\u9650<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u5e94\u7528\u62e5\u6709\u8bfb\u53d6\u5916\u90e8\u5b58\u50a8\u7684\u6743\u9650\u3002<\/li>\n<li>\u5728 androidmanifest.xml \u4e2d\u6dfb\u52a0\u6743\u9650 \u3002<\/li>\n<li>\u5728\u4ee3\u7801\u4e2d\u8bf7\u6c42\u6743\u9650 activitycompat.requestpermissions()\u3002<\/li>\n<\/ul>\n<p><strong>2. \u6784\u5efa\u7528\u4e8e\u6253\u5f00 word \u6587\u4ef6\u7684 intent<\/strong><\/p>\n<ul>\n<li>\n<p>\u786e\u4fdd intent \u6b63\u786e\u6784\u5efa\uff1a<\/p>\n<pre>public intent getwordfileintent(string filepath) {\n  file file = new file(filepath);\n  uri fileuri = fileprovider.geturiforfile(this, getpackagename() + \".fileprovider\", file);\n  \n  intent intent = new intent(intent.action_view);\n  intent.setdataandtype(fileuri, \"application\/msword\");\n  intent.addflags(intent.flag_grant_read_uri_permission); \/\/ \u6388\u4e88 uri \u8bfb\u53d6\u6743\u9650\n  return intent;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>3. \u8bbe\u7f6e fileprovider<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e android 7.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff0c\u4f7f\u7528 fileprovider \u5b89\u5168\u5730\u5171\u4eab\u6587\u4ef6 uri\u3002<\/li>\n<li>\u5728 androidmanifest.xml \u4e2d\u6dfb\u52a0 fileprovider \u5b9a\u4e49\u3002<\/li>\n<li>\u521b\u5efa file_paths.xml \u6587\u4ef6\u4ee5\u6307\u5b9a\u5171\u4eab\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><strong>4. \u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528 try-catch \u6355\u83b7\u5f02\u5e38\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>try {\n  startActivity(intent);\n} catch (ActivityNotFoundException e) {\n  \/\/ \u5904\u7406\u6ca1\u6709\u5e94\u7528\u53ef\u4ee5\u6253\u5f00\u6b64\u6587\u4ef6\u7684\u5f02\u5e38\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u8865\u5145\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li>\u5e94\u7528\u7a0b\u5e8f\u8df3\u8f6c\u540e\u4f1a\u8fdb\u5165 looper \u6d88\u606f\u961f\u5217\u7684\u539f\u56e0\u53ef\u80fd\u662f\uff0cstartactivity(intent)\u653e\u5165\u4e86\u4e00\u4e2a\u4efb\u52a1\u5230\u6d88\u606f\u961f\u5217\uff0c\u800c\u8fd9\u4e2a\u4efb\u52a1\u9700\u8981\u7b49\u5f85\u6267\u884c\uff0c\u8fdb\u5165 looper \u6d88\u606f\u961f\u5217\u662f\u4e3a\u4e86\u4e0d\u65ad\u5730\u68c0\u67e5\u6d88\u606f\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u662f\u5426\u53ef\u4ee5\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5b89\u5353\u6253\u5f00 Word \u6587\u6863\u65f6\u51fa\u9519\uff0c\u5982\u4f55\u89e3\u51b3\u542f\u52a8 Activity \u5931\u8d25\u95ee\u9898\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\u6863\u65f6\u51fa\u9519 \u95ee\u9898\u63cf\u8ff0 \u6267\u884c startactivity(intent); \u540e\uff0c\u7a0b\u5e8f\u81ea\u52a8\u5173\u95ed\u3002\u6587\u4ef6\u8def\u5f84\u4e3a &#8220;\/storage\/emulated\/0\/\u68c0\u6d4b\u62a5\u544a\/20240919151923_\u68c0\u6d4b\u8bb0\u5f55.docx&#8221;\u3002 \u89e3\u51b3\u65b9\u6848 1. \u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u7684\u6743\u9650 \u786e\u4fdd\u5e94\u7528\u62e5\u6709\u8bfb\u53d6\u5916\u90e8\u5b58\u50a8\u7684\u6743\u9650\u3002 \u5728 androidmanifest.xml \u4e2d\u6dfb\u52a0\u6743\u9650 \u3002 \u5728\u4ee3\u7801\u4e2d\u8bf7\u6c42\u6743\u9650 activitycompat.requestpermissions()\u3002 2. \u6784\u5efa\u7528\u4e8e\u6253\u5f00 word \u6587\u4ef6\u7684 intent \u786e\u4fdd intent \u6b63\u786e\u6784\u5efa\uff1a public intent getwordfileintent(string filepath) { file file = new file(filepath); uri fileuri = fileprovider.geturiforfile(this, getpackagename() + &#8220;.fileprovider&#8221;, file); intent intent = new intent(intent.action_view); intent.setdataandtype(fileuri, &#8220;application\/msword&#8221;); intent.addflags(intent.flag_grant_read_uri_permission); \/\/ \u6388\u4e88 uri [&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-36530","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36530","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=36530"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36530\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}