1. 程式人生 > >安卓中圖片和Base64編碼字符集的相互轉換

安卓中圖片和Base64編碼字符集的相互轉換

前言:有幾天沒寫部落格了,最近趕專案,還有好哥們的婚禮加在一起,實在是忙壞了。說起好哥們結婚,就想多說幾句。想必大家和我一樣,在學校時有那麼幾個臭味相投的哥們,不多也不算少,一起上課,吃飯,去圖書館(其實次數很少),打球,玩遊戲,通宵,談人生理想(其實就是瞎扯淡)等等,天天膩在一起,沒心沒肺,打打鬧鬧,直到畢業還覺得時間太少。所以就有這麼一個約定:“”我結婚的時候你一定要來哦,這樣我比較有安全感。。。“”,想吐就吐吧,別憋著。上次聚在一起是其中一個山東臨沂的結婚,算算已經兩年半了,當時我還在天津上班,離山東不太遠,和天津的一個同學一起去一起回的,那時候畢業才兩年,感觸沒那麼深,就是覺得見面的機會越來越少。這次是連雲港東海縣的同學,三月一號通知的我們四月2號結婚,挺想他們,商量下時間就直接買票了。去了三天,說出來也不怕笑話,一號下午從成都走,四號凌晨回的成都,呆了三天,除了喝酒,在賓館休息的時候就一起打王者榮耀,等車時候開小時房打,也吵架,說這不對說那不好的,玩得還挺開心,一點不覺得無聊。下個結婚的不知道是誰,希望快點。

不好意思扯得有點遠了。

言歸正傳,專案中遇到圖片上傳的問題,需要將圖片轉換成Base64編碼的字符集,伺服器端自己處理。只知道java中有提供這個API,但是需要匯入jar包,安卓中不知道有沒有,也沒用過,直接在Studio中打出Base64,有個Base64的類點進去看看,一看有decode,encode,還有編碼字串,不用說,就是它了。看看方法介紹就開始測試。下面把Base64這個類的介紹貼出來,這是andriod.util包下的,有興趣的同學可以進去看看。

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.util;

import java.io.UnsupportedEncodingException;

/**
 * Utilities for encoding and decoding the Base64 representation of
 * binary data.  See RFCs <a
 * href="http://www.ietf.org/rfc/rfc2045.txt">2045</a> and <a
 * href="http://www.ietf.org/rfc/rfc3548.txt">3548</a>.
 */
public class Base64 {
。。。。
}

這裡我主要用到裡面的兩個方法,decode和encode,然後封裝一下,便於重複使用。下面是我封裝的程式碼,很簡單,相信大家一看就明白,直接上程式碼。

將圖片轉換成Base64編碼的字串

 /**
     * 將圖片轉換成Base64編碼的字串
     * @param path
     * @return base64編碼的字串
     */
    public static String imageToBase64(String path){
        if(TextUtils.isEmpty(path)){
            return null;
        }
        InputStream is = null;
        byte[] data = null;
        String result = null;
        try{
            is = new FileInputStream(path);
            //建立一個字元流大小的陣列。
            data = new byte[is.available()];
            //寫入陣列
            is.read(data);
            //用預設的編碼格式進行編碼
            result = Base64.encodeToString(data,Base64.DEFAULT);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(null !=is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

//base64編碼字符集轉化成圖片檔案

 /**
     *base64編碼字符集轉化成圖片檔案。
     * @param base64Str
     * @param path 檔案儲存路徑
     * @return 是否成功
     */
    public static boolean base64ToFile(String base64Str,String path){
        byte[] data = Base64.decode(base64Str,Base64.DEFAULT);
        for (int i = 0; i < data.length; i++) {
            if(data[i] < 0){
                //調整異常資料
                data[i] += 256;
            }
        }
        OutputStream os = null;
        try {
            os = new FileOutputStream(path);
            os.write(data);
            os.flush();
            os.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }catch (IOException e){
            e.printStackTrace();
            return false;
        }

    }





好了,是不是很easy,今天的學習就結束了,謝謝大家支援。