在Android中将base64字符串转换为图像
有没有办法在Android中将base64字符串转换为图像?我正在从通过套接字连接的服务器接收这个xml格式的base64字符串。看一看将base64字符串转换为字节数组的示例,然后使用在Android中将base64字符串转换为图像,android,image,base64,Android,Image,Base64,有没有办法在Android中将base64字符串转换为图像?我正在从通过套接字连接的服务器接收这个xml格式的base64字符串。看一看将base64字符串转换为字节数组的示例,然后使用ImageIcon(byte[]imageData)构造函数。Android中现在有一些实用程序,但它们只在Android OS 2.2上可用。在没有任何解决方案(甚至在Stackoverflow上)后,我构建了一个插件,将Base64 PNG字符串转换为我共享的文件。 希望对您有所帮助。如果您想将base64字
ImageIcon(byte[]imageData)
构造函数。Android中现在有一些实用程序,但它们只在Android OS 2.2上可用。在没有任何解决方案(甚至在Stackoverflow上)后,我构建了一个插件,将Base64 PNG字符串转换为我共享的文件。
希望对您有所帮助。如果您想将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" />