﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-遇铭思源 情意无限</title><link>http://www.blogjava.net/cookiewyz/</link><description /><language>zh-cn</language><lastBuildDate>Thu, 30 Apr 2026 14:11:01 GMT</lastBuildDate><pubDate>Thu, 30 Apr 2026 14:11:01 GMT</pubDate><ttl>60</ttl><item><title>android bitmap 浅谈</title><link>http://www.blogjava.net/cookiewyz/archive/2012/02/09/369674.html</link><dc:creator>cookiewyz</dc:creator><author>cookiewyz</author><pubDate>Thu, 09 Feb 2012 06:53:00 GMT</pubDate><guid>http://www.blogjava.net/cookiewyz/archive/2012/02/09/369674.html</guid><wfw:comment>http://www.blogjava.net/cookiewyz/comments/369674.html</wfw:comment><comments>http://www.blogjava.net/cookiewyz/archive/2012/02/09/369674.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cookiewyz/comments/commentRss/369674.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cookiewyz/services/trackbacks/369674.html</trackback:ping><description><![CDATA[需求：从服务器下载一张图片，显示在ImageView控件上，并将该图片保存在移动设备的SD上。<br />步骤：<br />（一）获得输入流<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //urlPath:服务器路径;<br />&nbsp; &nbsp; &nbsp; &nbsp; public InputStream getUrlInputStream(String urlPath) throws IOException{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL url=new URL(urlPath);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpURLConnection conn=(HttpURLConnection) url.openConnection();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream in=conn.getInputStream();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(in!=null){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return in;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("test", "输入流对象为空");<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />（二）将输入流转化为Bitmap流<br />public Bitmap getMyBitmap(InputStream in){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap bitmap=null;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(in!=null){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmap=BitmapFactory.decodeStream(in);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //BitmapFactory的作用：create Bitmap objects from various sources,including files,streams and byte-arrays;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return bitmap;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("test", "输入流对象in为空");<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />（三）给ImageView对象赋值<br />public void setWidgetImage(Bitmap bitmap){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageView img=new ImageView(this);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(bitmap!=null){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img.setImageBitmap(bitmap);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />（四）获取SD卡上的文件存储路径<br />public void createSDFile(){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File sdroot=Environment.getExternalStorageDirectory();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file=new File(sdroot+"/Android/date/包名/文件名");<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /**<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 相关操作<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br /><font color="red">注意:SD卡的权限</font><br />（五）将图片保存到SD卡上<br />public boolean readToSDCard(File file,Bitmap bitmap) throws FileNotFoundException{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileOutputStream os=new FileOutputStream(file);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return bitmap.compress(Bitmap.CompressFormat.PNG, 90, os);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //bitmap.compress()的作用:write a compressed version of the bitmap to the specified outputstream;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //true:表示操作成功，false:表示操作失败<br />&nbsp; &nbsp; &nbsp; &nbsp; } <img src ="http://www.blogjava.net/cookiewyz/aggbug/369674.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cookiewyz/" target="_blank">cookiewyz</a> 2012-02-09 14:53 <a href="http://www.blogjava.net/cookiewyz/archive/2012/02/09/369674.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>