遇銘思源 情意無限

          常用鏈接

          統計

          最新評論

          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)編輯 收藏

          主站蜘蛛池模板: 宁陵县| 新建县| 江陵县| 宜兰市| 白山市| 纳雍县| 宝鸡市| 灵宝市| 宁河县| 阿坝县| 黔南| 霍山县| 富顺县| 奎屯市| 厦门市| 平邑县| 景泰县| 陈巴尔虎旗| 手机| 专栏| 山阴县| 北京市| 辽阳县| 望奎县| 临朐县| 兰西县| 买车| 武隆县| 永泰县| 辉南县| 腾冲县| 眉山市| 浙江省| 资讯 | 罗田县| 盐边县| 当阳市| 赞皇县| 南江县| 西乌珠穆沁旗| 鄂伦春自治旗|