2014-01-10 21 views
2

我將我的圖像轉換爲字節數組然後轉換爲base64字符串,它完全轉化並解碼,但是當我使用php將該字符串保存到mysql數據庫並從數據庫中檢索它時,它不會解碼並說錯誤base64在android中使用Base64

這是我的代碼。

PHP

$sql = "insert into users(username, password, email,mob,imagetext) 
          values ('".$username."', '".$password."', '".$email."', '".$mob."', '".$imageText."') ";        

機器人

編碼

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
Bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
byte[] b = baos.toByteArray(); 
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT); 

解碼

byte[] decodedByte = Base64.decode(value, Base64.DEFAULT);      
b = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
+0

我認爲它包裝內的MySQL保存之前的字符串。 – Punit

+0

你不提供PHP的解碼功能...... – Leonardo

+0

我將作爲使用PHP編碼和解碼是由Android代碼來處理文本。那是正確的方法嗎?我是編碼新手! – Punit

回答

0

嘗試改變從Base64.DEFAULT Base64編碼選項。 您應該使用Base64.URL_SAFE,它將使用發送到您的php腳本時不需要進行url編碼的字符。 還可以考慮使用Base64.NO_WRAP,它可以防止將MIME新行添加到base 64輸出中。

0

基於上面的回答, 編碼:

String imageEncoded = Base64.encodeToString(b,Base64.NO_WRAP | Base64.URL_SAFE); 

解碼:

byte[] decodedByte = Base64.decode(value, Base64.NO_WRAP | Base64.URL_SAFE);  
相關問題