2010-05-25 132 views
6

有沒有什麼辦法可以將base64字符串轉換爲Android中的圖像?我在通過套接字連接的服務器的xml中接收到這個base64字符串。將base64字符串轉換爲Android中的圖像

+1

可能重複的[如何將圖像轉換爲Base64字符串?](http://stackoverflow.com/questions/4830711/how-to-convert-一個圖像 - 到 - 的base64〜應變G) – 2012-07-09 10:19:38

回答

2

現在在Android中有Base64實用程序,但它們僅適用於Android OS 2.2。

0

無法獲得任何解決方案(即使在Stackoverflow上)後,我構建了一個插件,將Base64 PNG字符串轉換爲我共享的文件here。 希望有所幫助。

0

如果你想的base64字符串轉換爲圖像文件(例如.png格式等),並將其保存到某個文件夾,你可以使用此代碼:

byte[] btDataFile = Base64.decode(base64Image, Base64.DEFAULT); 
String fileName = YOUR_FILE_NAME + ".png"; 
try { 

    File folder = new File(context.getExternalFilesDir("") + /PathToFile); 
    if(!folder.exists()){ 
    folder.mkdirs(); 
    } 

    File myFile = new File(folder.getAbsolutePath(), fileName); 
    myFile.createNewFile(); 

    FileOutputStream osf = new FileOutputStream(myFile); 
    osf.write(btDataFile); 
    osf.flush(); 
    osf.close(); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

並確保您已在您的清單文件中給出了以下所需權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
相關問題