遇銘思源 情意無限

          常用鏈接

          統(tǒng)計

          最新評論

          android bitmap 淺談

          需求:從服務(wù)器下載一張圖片,顯示在ImageView控件上,并將該圖片保存在移動設(shè)備的SD上。
          步驟:
          (一)獲得輸入流
                          //urlPath:服務(wù)器路徑;
                  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;
                          }
                  }
          (二)將輸入流轉(zhuǎn)化為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())){
                                  /**
                                   * 相關(guān)操作
                                   */
                          }
                  }
          注意:SD卡的權(quán)限
          (五)將圖片保存到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 閱讀(456) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 沙湾县| 东台市| 三门峡市| 晋中市| 佳木斯市| 馆陶县| 青州市| 宁南县| 镇沅| 湘潭市| 年辖:市辖区| 龙门县| 望谟县| 北流市| 德庆县| 乐陵市| 龙川县| 巩义市| 方山县| 垣曲县| 甘德县| 正安县| 莆田市| 军事| 金堂县| 永泰县| 丰宁| 卢氏县| 桃园市| 临邑县| 元谋县| 托克托县| 仁布县| 清苑县| 招远市| 榕江县| 城固县| 尖扎县| 遵化市| 娄烦县| 元江|