遇銘思源 情意無限

          常用鏈接

          統計

          最新評論

          2012年2月9日 #

          android bitmap 淺談

          需求:從服務器下載一張圖片,顯示在ImageView控件上,并將該圖片保存在移動設備的SD上。
          步驟:
          (一)獲得輸入流
                          //urlPath:服務器路徑;
                  public InputStream getUrlInputStream(String urlPath) throws IOException{
                          URL url=new URL(urlPath);
                          HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                          InputStream in=conn.getInputStream();
                          if(in!=null){
                                  return in;
                          }else{
                                  Log.i("test", "輸入流對象為空");
                                  return null;
                          }
                  }
          (二)將輸入流轉化為Bitmap流
          public Bitmap getMyBitmap(InputStream in){
                          Bitmap bitmap=null;
                          if(in!=null){
                                  bitmap=BitmapFactory.decodeStream(in);
                                  //BitmapFactory的作用:create Bitmap objects from various sources,including files,streams and byte-arrays;
                                  return bitmap;
                          }else{
                                  Log.i("test", "輸入流對象in為空");
                                  return null;
                          }
                  }
          (三)給ImageView對象賦值
          public void setWidgetImage(Bitmap bitmap){
                          ImageView img=new ImageView(this);
                          if(bitmap!=null){
                                  img.setImageBitmap(bitmap);
                          }
                  }
          (四)獲取SD卡上的文件存儲路徑
          public void createSDFile(){
                          File sdroot=Environment.getExternalStorageDirectory();
                          File file=new File(sdroot+"/Android/date/包名/文件名");
                          if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
                                  /**
                                   * 相關操作
                                   */
                          }
                  }
          注意:SD卡的權限
          (五)將圖片保存到SD卡上
          public boolean readToSDCard(File file,Bitmap bitmap) throws FileNotFoundException{
                          FileOutputStream os=new FileOutputStream(file);
                          return bitmap.compress(Bitmap.CompressFormat.PNG, 90, os);
                          //bitmap.compress()的作用:write a compressed version of the bitmap to the specified outputstream;
                          //true:表示操作成功,false:表示操作失敗
                  }

          posted @ 2012-02-09 14:53 cookiewyz 閱讀(456) | 評論 (0)編輯 收藏

          僅列出標題  
          主站蜘蛛池模板: 河北省| 磴口县| 武邑县| 新建县| 阿拉善左旗| 阿拉善盟| 祁阳县| 武功县| 石景山区| 和林格尔县| 长垣县| 资中县| 长白| 家居| 旬邑县| 柳江县| 且末县| 秦皇岛市| 肃北| 琼结县| 茌平县| 阿鲁科尔沁旗| 佳木斯市| 牡丹江市| 加查县| 安溪县| 石林| 石台县| 秦安县| 汉川市| 曲周县| 东平县| 鄯善县| 涿鹿县| 盖州市| 兴国县| 唐河县| 宝兴县| 普定县| 庄浪县| 修文县|