有没有办法在Android中将base64字符串转换为图像?我从通过套接字连接的服务器中以xml格式收到这个base64字符串。
请查看http://www.source-code.biz/base64coder/java/或任何其他将base64字符串转换为字节数组的示例,然后使用ImageIcon(byte[] imageData)
构造函数。
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" />