Android 圖片轉換爲 Base64 編碼

 

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

    /**
     * 將圖片轉換成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 (Exception e){
            e.printStackTrace();
        }finally {
            if(null !=is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
        }
        return result;
    }

參考:https://blog.csdn.net/qq_35372900/article/details/69950867

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章