Convert base64 String to image on Android

Is there a way to convert a base64 String to an image on Android? I get this base64 String in xml from a server connected through a socket.

+6
android image base64
source share
4 answers

Now there are Base64 utilities on Android, but they became available only with Android 2.2.

+2
source share

See the 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.

+1
source share

After getting any solutions (even in Stackoverflow), I created a plugin that converts Base64 PNG Strings to files that I shared here . Hope this helps.

0
source share

If you want to convert the base64 string to an image file (e.g. .png , etc.) and save it in 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 that you provide the following required permissions in the manifest file:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
0
source share

All Articles