{"id":955,"date":"2015-08-14T09:53:51","date_gmt":"2015-08-14T01:53:51","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=955"},"modified":"2015-08-14T10:29:19","modified_gmt":"2015-08-14T02:29:19","slug":"android%e5%8a%a0%e8%bd%bd%e7%bd%91%e7%bb%9c%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=955","title":{"rendered":"Android\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247"},"content":{"rendered":"<p>Android\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\u8d44\u6e90\u65b9\u5f0f\u548c\u52a0\u8f7d\u5b57\u7b26\u4e32\u8d44\u6e90\u7c7b\u5f0f\uff0c\u4e5f\u662f\u901a\u8fc7Http\u8fde\u63a5\u89e3\u6790\u8fd4\u56de\u7684\u5b57\u8282\u6d41InputStream\u5b9e\u73b0<br \/>\n<!--more--><\/p>\n<p>[java]\/\/\u6784\u9020URL\u7c7b<br \/>\nURL url = new URL(httpUrl);<br \/>\n\/\/\u4eceurl\u4e2d\u5f97\u5230\u56fe\u7247\u8f93\u5165\u6d41<br \/>\nInputStream in = url.openStream();<br \/>\n\/\/\u4f7f\u7528BitmapFactory\u7c7b\u89e3\u6790\u6d41\u5f97\u5230Bitmap\u5bf9\u8c61<br \/>\nBitmap bitmap = BitmapFactory.decodeStream(in);[\/java]<\/p>\n<p>\u6ce8:InputStream in = url.openStream()<br \/>\n\u7b49\u4ef7\u4e8e<br \/>\nHttpURLConnection conn = (HttpURLConnection) url.openConnection();<br \/>\nInputStream is = conn.getInputStream();<\/p>\n<p>openStream()\u65b9\u6cd5\u5176\u5b9e\u662f\u5bf9\u4e0a\u9762\u4e24\u884c\u4ee3\u7801\u7684\u5c01\u88c5<\/p>\n<p>[java] \/**<br \/>\n  * Equivalent to {@code openConnection().getInputStream(types)}.<br \/>\n  *\/<br \/>\n public final InputStream openStream() throws IOException {<br \/>\n     return openConnection().getInputStream();<br \/>\n}[\/java]<\/p>\n<p>\u63a5\u7740\u6211\u4eec\u6765\u5b9e\u73b0\u4e00\u4e2a\u7f51\u7edc\u52a0\u8f7d\u5355\u5f20\u56fe\u7247\u5b9e\u4f8b<br \/>\n\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u6839\u636e\u56fe\u7247\u5730\u5740\u83b7\u53d6\u56fe\u7247<\/p>\n<p>[java]public Bitmap downLoadBitmap(String httpUrl) {<br \/>\n\t\tInputStream in = null;<br \/>\n\t\ttry {<br \/>\n\t\t\tURL url = new URL(httpUrl);<br \/>\n\t\t\tin = url.openStream();<br \/>\n\t\t\tBitmap bitmap = BitmapFactory.decodeStream(in);<br \/>\n\t\t\treturn bitmap;<\/p>\n<p>\t\t} catch (MalformedURLException e) {<br \/>\n\t\t\te.printStackTrace();<br \/>\n\t\t} catch (IOException e) {<br \/>\n\t\t\te.printStackTrace();<br \/>\n\t\t} finally {<br \/>\n\t\t\tif (in != null) {<br \/>\n\t\t\t\ttry {<br \/>\n\t\t\t\t\tin.close();<br \/>\n\t\t\t\t} catch (IOException e) {<br \/>\n\t\t\t\t\te.printStackTrace();<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t\treturn null;<br \/>\n\t}[\/java]<\/p>\n<p>\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\u662f\u8017\u65f6\u64cd\u4f5c\u6211\u4eec\u9700\u8981\u5728\u5f02\u6b65\u4efb\u52a1\u4e2d\u8c03\u7528<\/p>\n<p>[java]<br \/>\nString httpUrl = &quot;http:\/\/d.hiphotos.baidu.com\/image\/h%3D360\/sign=<br \/>\nba3bf5f839c79f3d90e1e2368aa1cdbc\/f636afc379310a55c01f6ef3b54543a9822610f9.jpg&quot;;<\/p>\n<p>\t\t\tnew AsyncTask&lt;String, Void, Bitmap&gt;() {<br \/>\n\t\t\t\tprotected void onPreExecute() {<br \/>\n\t\t\t\t\tmProgressBar.setVisibility(View.VISIBLE);<br \/>\n\t\t\t\t}<\/p>\n<p>\t\t\t\t@Override<br \/>\n\t\t\t\tprotected Bitmap doInBackground(String&#8230; params) {<br \/>\n\t\t\t\t\tString httpUrl = params[0];<br \/>\n\t\t\t\t\tBitmap bitmap = downLoadBitmap(httpUrl);<\/p>\n<p>\t\t\t\t\treturn bitmap;<br \/>\n\t\t\t\t}<\/p>\n<p>\t\t\t\t@Override<br \/>\n\t\t\t\tprotected void onPostExecute(Bitmap result) {<br \/>\n\t\t\t\t\tmProgressBar.setVisibility(View.GONE);<br \/>\n\t\t\t\t\tif (result != null)<br \/>\n\t\t\t\t\t\tmShowImg.setImageBitmap(result);<\/p>\n<p>\t\t\t\t}<br \/>\n\t\t\t}.execute(httpUrl);[\/java]<\/p>\n<p>\u754c\u9762\u90e8\u4efd\u4f7f\u7528imageView\u63a7\u4ef6\uff0cProgressBar\u663e\u793a\u52a0\u8f7d\u4e2d\u6548\u679c\u5982\u4e0b<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/imageloader1.gif\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-956\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/imageloader1.gif\" alt=\"imageloader1\" width=\"376\" height=\"500\" \/><\/a><\/p>\n<p><strong>\u63a5\u7740\u6211\u4eec\u5728\u6765\u770b\u4e0b\u5b9e\u73b0\u591a\u56fe\u52a0\u8f7d\u64cd\u4f5c<\/strong><br \/>\n\u83b7\u53d6\u56fe\u7247\u8d44\u6e90\u65b9\u5f0f\u548c\u5355\u5f20\u56fe\u7247\u4e00\u6837\uff0c\u663e\u793a\u63a7\u4ef6\u6211\u4eec\u6362\u7528GridView.<br \/>\n\u4e3b\u8981\u4ee3\u7801\u5982\u4e0b<br \/>\n[java]@Override<br \/>\npublic View getView(int position, View convertView, ViewGroup parent) {<\/p>\n<p>\tfinal ImageView loaderImageView;<\/p>\n<p>\tif (convertView == null) {<br \/>\n\t\tconvertView = mInflater.inflate(R.layout.item_my_gridview_layout, null);<\/p>\n<p>\t\tloaderImageView = (ImageView) convertView.findViewById(R.id.my_gridview_img);<br \/>\n\t\tconvertView.setTag(loaderImageView);<br \/>\n\t} else {<br \/>\n\t\tloaderImageView = (ImageView) convertView.getTag();<br \/>\n\t}<\/p>\n<p>\tString itemUrl = (String) getItem(position);<\/p>\n<p>\tnew AsyncTask&lt;String, Void, Bitmap&gt;() {<\/p>\n<p>\t\t@Override<br \/>\n\t\tprotected Bitmap doInBackground(String&#8230; params) {<br \/>\n\t\t\tString httpUrl = params[0];<br \/>\n\t\t\t\/\/\u6839\u636ehttpUrl\u5730\u5740\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247<br \/>\n\t\t\t\tBitmap bitmap = downLoadBitmap(httpUrl);<br \/>\n\t\t\t\treturn bitmap;<br \/>\n\t\t\t}<\/p>\n<p>\t\t\t@Override<br \/>\n\t\t\tprotected void onPostExecute(Bitmap result) {<\/p>\n<p>\t\t\t\tif (result != null)<br \/>\n\t\t\t\t\tloaderImageView.setImageBitmap(result);<\/p>\n<p>\t\t\t}<br \/>\n\t\t}.execute(itemUrl);<\/p>\n<p>\t\treturn convertView;<br \/>\n\t}<\/p>\n<p>}[\/java]<\/p>\n<p>\u6548\u679c\u5982\u4e0b:<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/imageloader2.gif\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/imageloader2.gif\" alt=\"imageloader2\" width=\"365\" height=\"615\" class=\"alignnone size-full wp-image-961\" \/><\/a><\/p>\n<p>\u5c06AsyncTask\u653e\u5230\u9002\u914d\u5668getView\u65b9\u6cd5\u83b7\u53d6\u56fe\u7247\u4e00\u5c4f\u7684\u65f6\u5019\u611f\u89c9\u4e0d\u5230\u95ee\u9898\uff0c\u5f53\u6211\u4eec\u56fe\u7247\u5f88\u591a\u65f6\uff0c\u4e0a\u4e0b\u6eda\u52a8\uff0c\u8fd9\u65f6\u4f1a\u53d1\u73b0\u56fe\u7247\u9519\u4f4d\u73b0\u8c61.<br \/>\n\u8fd9\u662f\u56e0\u4e3a\u4e0a\u4e0b\u6eda\u52a8\u65f6\u4f1a\u4e0d\u4f46\u91cd\u590d\u8c03\u7528getView\u65b9\u6cd5\u542f\u52a8AsyncTask.\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6211\u4eec\u53ef\u4ee5\u5c06AsyncTask\u8fdb\u884c\u4fdd\u5b58\uff0c\u6839\u636eUrl\u6765\u63a7\u5236\u662f\u5426\u542f\u52a8AsyncTask,\u5177\u4f53\u4ee3\u7801\u5982\u4e0b:<br \/>\n[java]<br \/>\npublic void loadBitmap(Resources res,String imageUrl, ImageView imageView, int resId) {<br \/>\n\tif (cancelPotentialWork(imageUrl, imageView)) {<br \/>\n\t\tBitmapWorkerTask task = new BitmapWorkerTask(imageView);<\/p>\n<p>\t\tAsyncDrawable asyncDrawable = new AsyncDrawable(res,<br \/>\n\t\t\t\tBitmapFactory.decodeResource(res, resId), task);<br \/>\n\t\timageView.setImageDrawable(asyncDrawable);<\/p>\n<p>\t\ttask.execute(imageUrl);<\/p>\n<p>\t}<\/p>\n<p>}<\/p>\n<p>public static boolean cancelPotentialWork(String imageUrl,<br \/>\n\t\tImageView imageView) {<br \/>\n\tfinal BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);<\/p>\n<p>\tif (bitmapWorkerTask != null) {<br \/>\n\t\tfinal String bitmapData = bitmapWorkerTask.imageUrl;<br \/>\n\t\tif (!bitmapData.equals(imageUrl)) {<br \/>\n\t\t\t\/\/ Cancel previous task<br \/>\n\t\t\tbitmapWorkerTask.cancel(true);<br \/>\n\t\t} else {<br \/>\n\t\t\t\/\/ The same work is already in progress<br \/>\n\t\t\treturn false;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\t\/\/ No task associated with the ImageView, or an existing task was<br \/>\n\t\/\/ cancelled<br \/>\n\treturn true;<br \/>\n}<\/p>\n<p>private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {<br \/>\n\tif (imageView != null) {<br \/>\n\t\tfinal Drawable drawable = imageView.getDrawable();<br \/>\n\t\tif (drawable instanceof AsyncDrawable) {<br \/>\n\t\t\tfinal AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;<br \/>\n\t\t\treturn asyncDrawable.getBitmapWorkerTask();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\treturn null;<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \u521b\u5efa\u4e00\u4e2a\u4e13\u7528\u7684Drawable\u7684\u5b50\u7c7b\u6765\u50a8\u5b58\u8fd4\u56de\u5de5\u4f5c\u4efb\u52a1\u7684\u5f15\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5f53\u4efb\u52a1\u5b8c\u6210\u65f6BitmapDrawable\u4f1a\u88ab\u4f7f\u7528<br \/>\n *<br \/>\n *\/<br \/>\nstatic class AsyncDrawable extends BitmapDrawable {<br \/>\n\tprivate final WeakReference&lt;BitmapWorkerTask&gt; bitmapWorkerTaskReference;<\/p>\n<p>\tpublic AsyncDrawable(Resources res, Bitmap bitmap,<br \/>\n\t\t\tBitmapWorkerTask bitmapWorkerTask) {<br \/>\n\t\tsuper(res, bitmap);<br \/>\n\t\tbitmapWorkerTaskReference = new WeakReference&lt;BitmapWorkerTask&gt;(<br \/>\n\t\t\t\tbitmapWorkerTask);<br \/>\n\t}<\/p>\n<p>\tpublic BitmapWorkerTask getBitmapWorkerTask() {<br \/>\n\t\treturn (BitmapWorkerTask) bitmapWorkerTaskReference.get();<br \/>\n\t}<br \/>\n}<\/p>\n<p>class BitmapWorkerTask extends AsyncTask&lt;String, Void, Bitmap&gt; {<br \/>\n\tprivate final WeakReference&lt;ImageView&gt; imageViewReference;<br \/>\n\tprivate String imageUrl = &quot;&quot;;<\/p>\n<p>\tpublic BitmapWorkerTask(ImageView imageView) {<br \/>\n\t\t\/\/ Use a WeakReference to ensure the ImageView can be garbage<br \/>\n\t\t\/\/ collected<br \/>\n\t\timageViewReference = new WeakReference&lt;ImageView&gt;(imageView);<br \/>\n\t}<\/p>\n<p>\t\/\/ Decode image in background.<br \/>\n\t@Override<br \/>\n\tprotected Bitmap doInBackground(String&#8230; params) {<br \/>\n\t\timageUrl = params[0];<br \/>\n\t\treturn downLoadBitmap(imageUrl);<br \/>\n\t}<\/p>\n<p>\t\/\/ Once complete, see if ImageView is still around and set bitmap.<br \/>\n\t@Override<br \/>\n\tprotected void onPostExecute(Bitmap bitmap) {<br \/>\n\t\tif (isCancelled()) {<br \/>\n\t\t\tbitmap = null;<br \/>\n\t\t}<\/p>\n<p>\t\tif (imageViewReference != null &amp;&amp; bitmap != null) {<br \/>\n\t\t\tfinal ImageView imageView = (ImageView) imageViewReference<br \/>\n\t\t\t\t\t.get();<br \/>\n\t\t\tfinal BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);<br \/>\n\t\t\tif (this == bitmapWorkerTask &amp;&amp; imageView != null) {<br \/>\n\t\t\t\timageView.setImageBitmap(bitmap);<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\u8d44\u6e90\u65b9\u5f0f\u548c\u52a0\u8f7d\u5b57\u7b26\u4e32\u8d44\u6e90\u7c7b\u5f0f\uff0c\u4e5f\u662f\u901a\u8fc7Http\u8fde\u63a5\u89e3\u6790\u8fd4\u56de\u7684\u5b57\u8282\u6d41InputStre [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/955"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=955"}],"version-history":[{"count":5,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/955\/revisions"}],"predecessor-version":[{"id":963,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/955\/revisions\/963"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}