【问题标题】:Bitmap to Base64 String位图到 Base64 字符串
【发布时间】:2017-01-05 02:06:00
【问题描述】:

我正在尝试使用以下代码将位图编码为 base64 字符串,但我总是得到错误的编码。

顺便说一下,filePath是图片文件路径。

public String encode(String filePath) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byte_arr = stream.toByteArray();
    return Base64.encodeToString(byte_arr, Base64.DEFAULT);
}

我如何知道我是否得到了错误的编码?我使用这个网站:

http://codebeautify.org/base64-to-image-converter

【问题讨论】:

  • 检查您是否为图像提供了正确的路径,否则代码看起来很好。
  • 图像的路径是正确的,我检查了它。我不知道怎么了
  • 您可以将Base64.NO_PADDING 托盘化而不是/ orred 到DEFAULT,即没有尾随=s。

标签: android bitmap base64 encode


【解决方案1】:

试试这个代码,它为我做了正确的编码。希望对你有帮助。

public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}

【讨论】:

    【解决方案2】:

    尝试使用 NO_WRAP 代替 DEFAULT

    String stringEncode = Base64.encodeToString(byte_arr, Base64.NO_WRAP);
        return stringEncode;
    

    并使用此 online tool 将 base64 字符串 (stringEncode) 转换为 image 。如果它能够生成图像你的base64字符串是正确的。

    【讨论】:

    • 读取文件路径时的选项配置是什么。是 ARGB_8888 吗? @Ofek
    【解决方案3】:

    试试这个:

    public String getImageEncodedData(String path) {
           File f = new File(path);
           Bitmap imageBitmap = null;
           try {
               imageBitmap = BitmapFactory.decodeStream(new FileInputStream(f));
           } catch (FileNotFoundException e) {
               e.printStackTrace();
           }
           //added for testing base 64
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           imageBitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
           byte[] imageBytes = baos.toByteArray();
           String mImageEncodedString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    //       Log.e("TAG", "imageEncodedString: " + mImageEncodedString);
           return mImageEncodedString;
       }
    

    【讨论】:

      【解决方案4】:

      正如@kdeogharkar 指出的那样,您的代码看起来不错。

      首先,检查您是否有正确的文件路径。然后检查访问外部文件时是否需要权限。

      为了在加载大尺寸图像时处理 OOM,我们可以使用:

      File sd = Environment.getExternalStorageDirectory();
      File image = new File(sd+filePath, imageName);
      BitmapFactory.Options bmOptions = new BitmapFactory.Options();
      Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
      bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
      

      然后使用下面的解码和编码方法:

      public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
      {
          ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
          image.compress(compressFormat, quality, byteArrayOS);
          return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
      }
      
      public static Bitmap decodeBase64(String input)
      {
          byte[] decodedBytes = Base64.decode(input, 0);
          return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
      }
      

      示例用法:

      String myBase64Image = encodeToBase64(myBitmap, Bitmap.CompressFormat.JPEG, 100);
      Bitmap myBitmapAgain = decodeBase64(myBase64Image);
      

      注意
      这个答案来自:

      1. https://stackoverflow.com/a/28351881/4758255
      2. https://stackoverflow.com/a/9768973/4758255

      【讨论】:

        猜你喜欢
        • 2012-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多