{"id":123,"date":"2023-02-18T11:01:37","date_gmt":"2023-02-18T03:01:37","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=123"},"modified":"2023-02-18T11:02:07","modified_gmt":"2023-02-18T03:02:07","slug":"android-html-fromhtml-parse-image-tag","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/android-html-fromhtml-parse-image-tag\/","title":{"rendered":"Android HTML.fromHtml\u89e3\u6790\u56fe\u7247\u6807\u7b7e"},"content":{"rendered":"<p>\u5728\u4e4b\u524dHtml\u7c7b\u652f\u6301\u7684HTML\u6807\u7b7e\u6587\u7ae0\u4e2d\u4e86\u89e3\u5230\u5f53\u89e3\u6790\u5230<code>&lt;img&gt;<\/code>\u6807\u7b7e\u65f6\u5c31\u4f1a\u56de\u8c03<code>getDrawable()<\/code>\u65b9\u6cd5\uff0c\u5e76\u9700\u8981\u8fd4\u56de\u4e00\u4e2aDrawable\u5bf9\u8c61\uff1b\u5f53\u524d\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u7c7b\u5e76\u5b9e\u73b0ImageGetter\u63a5\u53e3\u4ee5\u53ca\u5728<code>getDrawable()<\/code>\u65b9\u6cd5\u4e2d\u505a\u76f8\u5e94\u7684\u5904\u7406\uff0c\u4e0b\u9762\u6211\u4eec\u5219\u6765\u770b\u770b\u5177\u4f53\u8be5\u5982\u4f55\u5904\u7406\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/**\n * ImageGetter\u63a5\u53e3\u7684\u4f7f\u7528\n * @author yezhou\n *\/\npublic class ImgLabelActivity extends Activity {\n\n    private static final String TAG = &quot;ImgLabelActivity&quot;;\n    \/**\u672c\u5730\u56fe\u7247*\/\n    private TextView mTvOne;\n    \/**\u9879\u76ee\u8d44\u6e90\u56fe\u7247*\/\n    private TextView mTvTwo;\n    \/**\u7f51\u7edc\u56fe\u7247*\/\n    private TextView mTvThree;\n    \/**\u7f51\u7edc\u56fe\u7247name*\/\n    private String picName = &quot;networkPic.jpg&quot;;\n    \/**\u7f51\u7edc\u56fe\u7247Getter*\/\n    private NetworkImageGetter mImageGetter;\n    \/**\u7f51\u7edc\u56fe\u7247\u8def\u5f84*\/\n    private String htmlThree = &quot;\u7f51\u7edc\u56fe\u7247\u6d4b\u8bd5\uff1a&quot; + &quot;&lt;img src=&#039;http:\/\/www.yezhou.me\/images\/zhanghanyun.png&#039;&gt;&quot;;\n\n    @Override\n    public void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_img_label);\n\n        mTvOne = (TextView) this.findViewById(R.id.tv_img_label_one);\n        String htmlOne = &quot;\u672c\u5730\u56fe\u7247\u6d4b\u8bd5\uff1a&quot; + &quot;&lt;img src=&#039;\/mnt\/sdcard\/imgLabel.jpg&#039;&gt;&quot;;\n        mTvOne.setText(Html.fromHtml(htmlOne, new LocalImageGetter(), null));\n\n        mTvTwo = (TextView) this.findViewById(R.id.tv_img_label_two);\n        String htmlTwo = &quot;\u9879\u76ee\u56fe\u7247\u6d4b\u8bd5\uff1a&quot; + &quot;&lt;img src=\\&quot;&quot; + R.drawable.imagepro+&quot;\\&quot;&gt;&quot;;\n        mTvTwo.setText(Html.fromHtml(htmlTwo, new ProImageGetter(), null));\n\n        mTvThree = (TextView) this.findViewById(R.id.tv_img_label_three);\n        mImageGetter = new NetworkImageGetter();\n        mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));\n    }\n\n    \/**\n     * \u672c\u5730\u56fe\u7247\n     * @author yezhou\n     *\/\n    private final class LocalImageGetter implements Html.ImageGetter {\n        @Override\n        public Drawable getDrawable(String source) {\n            \/\/ \u83b7\u53d6\u672c\u5730\u56fe\u7247\n            Drawable drawable = Drawable.createFromPath(source);\n            \/\/ \u5fc5\u987b\u8bbe\u4e3a\u56fe\u7247\u7684\u8fb9\u9645,\u4e0d\u7136TextView\u663e\u793a\u4e0d\u51fa\u56fe\u7247\n            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());\n            \/\/ \u5c06\u5176\u8fd4\u56de\n            return drawable;\n        }\n    }\n\n    \/**\n     * \u9879\u76ee\u8d44\u6e90\u56fe\u7247\n     * @author yezhou\n     *\/\n    private final class ProImageGetter implements Html.ImageGetter {\n        @Override\n        public Drawable getDrawable(String source) {\n            \/\/ \u83b7\u53d6\u5230\u8d44\u6e90id\n            int id = Integer.parseInt(source);\n            Drawable drawable = getResources().getDrawable(id);\n            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());\n            return drawable;\n        }\n    }\n\n    \/**\n     * \u7f51\u7edc\u56fe\u7247\n     * @author yezhou \n     *\/\n    private final class NetworkImageGetter implements Html.ImageGetter {\n        @Override\n        public Drawable getDrawable(String source) {\n            Drawable drawable = null;\n            \/\/ \u5c01\u88c5\u8def\u5f84  \n            File file = new File(Environment.getExternalStorageDirectory(), picName);\n            \/\/ \u5224\u65ad\u662f\u5426\u4ee5http\u5f00\u5934\n            if(source.startsWith(&quot;http&quot;)) {\n                \/\/ \u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728\n                if(file.exists()) {\n                    \/\/ \u5b58\u5728\u5373\u83b7\u53d6drawable\n                    drawable = Drawable.createFromPath(file.getAbsolutePath());\n                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());\n                } else {\n                    \/\/ \u4e0d\u5b58\u5728\u5373\u5f00\u542f\u5f02\u6b65\u4efb\u52a1\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\n                    AsyncLoadNetworkPic networkPic = new AsyncLoadNetworkPic();\n                    networkPic.execute(source);\n                }\n            }\n            return drawable;\n        }\n    }\n\n    \/**\n     * \u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\u5f02\u6b65\u7c7b\n     * @author yezhou\n     *\/\n    private final class AsyncLoadNetworkPic extends AsyncTask&lt;String, Integer, Void&gt; {\n        @Override\n        protected Void doInBackground(String... params) {\n            \/\/ \u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\n            loadNetPic(params);\n            return null;\n        }\n\n        @Override\n        protected void onPostExecute(Void result) {\n            super.onPostExecute(result);\n            \/\/ \u5f53\u6267\u884c\u5b8c\u6210\u540e\u518d\u6b21\u4e3a\u5176\u8bbe\u7f6e\u4e00\u6b21  \n            mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));\n        }\n\n        \/**\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247*\/\n        private void loadNetPic(String... params) {\n            String path = params[0];\n            File file = new File(Environment.getExternalStorageDirectory(), picName);\n            InputStream in = null;\n            FileOutputStream out = null;\n            try {\n                URL url = new URL(path);\n                HttpURLConnection connUrl = (HttpURLConnection) url.openConnection();\n                connUrl.setConnectTimeout(5000);\n                connUrl.setRequestMethod(&quot;GET&quot;);\n                if(connUrl.getResponseCode() == 200) {\n                    in = connUrl.getInputStream();\n                    out = new FileOutputStream(file);\n                    byte[] buffer = new byte[1024];\n                    int len;\n                    while ((len = in.read(buffer))!= -1) {\n                        out.write(buffer, 0, len);\n                    }\n                } else {\n                    Log.i(TAG, connUrl.getResponseCode() + &quot;&quot;);\n                }\n            } catch (Exception e) {\n                e.printStackTrace();\n            } finally {\n                if (in != null) {\n                    try {\n                        in.close();\n                    } catch (IOException e) {\n                        e.printStackTrace();\n                    }\n                }\n                if (out != null) {\n                    try {\n                        out.close();\n                    } catch (IOException e) {\n                        e.printStackTrace();\n                    }\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<\/p>\n<ul>\n<li>\u5728\u83b7\u53d6\u5230drawable\u65f6\u9700\u8981\u4e3a\u5176\u8bbe\u7f6e\u8fb9\u754c\uff0c\u5982\u6ca1\u6709\u8bbe\u7f6e\u7684\u8bddTextView\u5c31\u4e0d\u80fd\u663e\u793a\u8be5drawable\uff1b<\/li>\n<li>\u5728\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\u65f6\u9700\u8981\u5f00\u542f\u5b50\u7ebf\u7a0b\u53bb\u8bbf\u95ee\u7f51\u7edc\u5e76\u5c06\u56fe\u7247\u5b58\u50a8\u5230\u672c\u5730\uff0c\u4e4b\u540e\u518d\u6b21\u4e3a\u5176\u8bbe\u7f6e\u4e00\u6b21\u8bfb\u53d6\u672c\u5730\u7684\u56fe\u7247\uff1b<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e4b\u524dHtml\u7c7b\u652f\u6301\u7684HTML\u6807\u7b7e\u6587\u7ae0\u4e2d\u4e86\u89e3\u5230\u5f53\u89e3\u6790\u5230&lt;img&gt;\u6807\u7b7e\u65f6\u5c31\u4f1a\u56de\u8c03getDrawabl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}