Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中将base64字符串转换为图像_Android_Image_Base64 - Fatal编程技术网

在Android中将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字

有没有办法在Android中将base64字符串转换为图像?我正在从通过套接字连接的服务器接收这个xml格式的base64字符串。

看一看将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" />