【问题标题】:Converting base64 String to image in Android在Android中将base64字符串转换为图像
【发布时间】:2010-05-25 06:47:39
【问题描述】:

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

【问题讨论】:

标签: android image base64


【解决方案1】:

查看http://www.source-code.biz/base64coder/java/ 或任何其他将base64 字符串转换为字节数组的示例,然后使用ImageIcon(byte[] imageData) 构造函数。

【讨论】:

    【解决方案2】:

    Android 中现在有 Base64 实用程序,但它们仅在 Android OS 2.2 中可用。

    【讨论】:

      【解决方案3】:

      在没有得到任何解决方案(甚至在 Stackoverflow 上)之后,我构建了一个插件,将 Base64 PNG 字符串转换为我共享的文件 here。 希望对您有所帮助。

      【讨论】:

        【解决方案4】:

        如果您想将 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" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-09-07
          • 2014-07-21
          • 2020-03-16
          • 1970-01-01
          • 1970-01-01
          • 2019-04-07
          • 2013-07-04
          • 2016-08-30
          相关资源
          最近更新 更多