6

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user291977
  • 237
  • 1
  • 5
  • 11
  • 1
    possible 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 Answers4

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

After failing to get any solutions (even on Stackoverflow), I built a plugin that converts Base64 PNG Strings to files which I shared here. Hope that helps.

Community
  • 1
  • 1
mukama
  • 969
  • 2
  • 12
  • 28
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