Is there any way that I can convert a base64 String to image in Android? I am receiving this base64 String in a xml from the server connected through socket.
            Asked
            
        
        
            Active
            
        
            Viewed 1.2k times
        
    6
            
            
        - 
                    1possible duplicate of [How to convert a image into Base64 string?](http://stackoverflow.com/questions/4830711/how-to-convert-a-image-into-base64-string) – Bo Persson Jul 09 '12 at 10:19
 
4 Answers
1
            
            
        Have a look at http://www.source-code.biz/base64coder/java/ or any other example that converts base64 strings to byte-arrays, and then use the ImageIcon(byte[] imageData) constructor.
        aioobe
        
- 413,195
 - 112
 - 811
 - 826
 
1
            
            
        There are now Base64 utilities in Android, but they only became available with Android OS 2.2.
        Vadim Kotov
        
- 8,084
 - 8
 - 48
 - 62
 
        ThomasW
        
- 16,981
 - 4
 - 79
 - 106
 
0
            
            
        If you want to convert base64 string to the image file (for example .png etc.) and save it to some folder you can use this code:
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();
} 
And make sure you have given the following required permission in your manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        valerybodak
        
- 4,195
 - 2
 - 42
 - 53