Android Base64編碼解碼

來源:互聯網
上載者:User

標籤:android   ar   os   for   java   on   檔案   資料   ef   

服務端圖片的資訊被轉化成字串,傳到android用戶端,android端需要把這些資訊再解碼轉化成圖片並儲存在本地。//編碼部分String string = Base64.encodeToString(str.getBytes(),Base64.DEFAULT);   //解碼部分string 是服務端發來的資訊byte[] byteIcon= Base64.decode(string,Base64.DEFAULT);for (int i = 0; i < byteIcon.length; ++i) {      if (byteIcon[i] < 0) {  byteIcon[i] += 256;   }}  //建立一個檔案對象File iconFile = new File("userIcon.png");FileOutputStream fos = new FileOutputStream(iconFile);if(!iconFile.exists()){ iconFile.creatNewFile();    }//把圖片資料寫入檔案形成圖片fos.write(byteIcon);我到網上搜了一下,發現我這種寫法變複雜了,實際上byteIcon可以直接轉換成Bitmap,不過這樣就不能實現本地儲存了//服務端//編碼,記住這個流,經常用於圖片和Base64資料的切換ByteArrayOutputStream baos = new ByteArrayOutputStream();//圖片壓縮並轉換成流,bitmap在這我就不初始化了bitmap.compress(Bitmap.compressFormat,100,baos);byte[] byteServer = baos.toByteArray();String result = Base64.encodeToString(byteServer.Base64.DEFAULT);[在此輸入連結描述][1]//android端byte[] byteIcon = Base64.decode(result,Base64.DEFAULT);Bitmap bitmap = BitmapFactory.decode(byteIcon,0,byteIcon.length);

Android Base64編碼解碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.