【问题标题】:Image to Base64 conversion issue图片到 Base64 的转换问题
【发布时间】:2014-11-29 06:21:56
【问题描述】:

我正在为图像到 base64 转换创建演示应用程序。一些图像正确转换 base64。当图像大小为 3.92Mb(4128*3096) 并转换为 base64 创建成功并上传到服务器但未上传时。 Base64 String写入文件后,文本文件大小为5.40Mb。

图像转换为base64的代码如下

public String convertImage(File file)  {

    FileInputStream inputSteam = new FileInputStream(file);
    inputSteam.read(getBytesFromFile(file));

    return Base64.encodeToString(getBytesFromFile(file, Base64.DEFAULT);
}


public byte[] getBytesFromFile(File file) throws IOException {

    InputStream is = new FileInputStream(file);
    // Get the size of the file
    long length = file.length();

    // You cannot create an array using a long type.
    // It needs to be an int type.
    // Before converting to an int type, check
    // to ensure that file is not larger than Integer.MAX_VALUE.
    if (length > Integer.MAX_VALUE) {

        // File is too large
    }

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int) length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {

        throw new IOException("Could not completely read file "+ file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}

如何处理这个问题。或任何其他转换图像的方式;

【问题讨论】:

    标签: android


    【解决方案1】:

    如果你使用 PHP 作为服务器,你是否在 PHP.ini 中检查了上传文件的最大大小

    【讨论】:

      【解决方案2】:

      base64 字符串文件应该大于原始文件大小,因为 base64 编码在字符串中添加了自己的字符以保持 base64 格式。所以这可能是文件大小大于原始大小的问题。

      【讨论】:

      • 任何其他处理图像的解决方案
      • link 请查看此链接可能会对您有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 2013-07-16
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多