遇銘思源 情意無限

          常用鏈接

          統計

          最新評論

          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 on 2012-02-09 14:53 cookiewyz 閱讀(458) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 临澧县| 湘潭县| 综艺| 禄劝| 榆树市| 若尔盖县| 北票市| 宽甸| 洪泽县| 宕昌县| 称多县| 临海市| 兖州市| 万全县| 科技| 呼图壁县| 永德县| 海丰县| 无锡市| 准格尔旗| 泰来县| 博湖县| 黑水县| 古蔺县| 宁化县| 安多县| 和龙市| 特克斯县| 新田县| 库尔勒市| 周口市| 白朗县| 兰坪| 光泽县| 鄱阳县| 新津县| 屏东市| 津南区| 土默特右旗| 华池县| 松原市|