【问题标题】:Bitmap to Base 64 Encode String位图到 Base 64 编码字符串
【发布时间】:2023-03-26 05:39:01
【问题描述】:

我正在尝试将图像转换为位图,然后将其编码为 base64 字符串。我在行上遇到空指针异常

 bMap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
            byteArrayBitmapStream);

怎么了?请注意,我将带有图片“pic2.jpg”的文件名输入到下面的方法中。

下面是我的代码:

    private String convertToBitmap (String name){
    File imgFile = new File (name);
    Bitmap bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    final int COMPRESSION_QUALITY = 100;
    String encodedImage;
    ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
    bMap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
            byteArrayBitmapStream);
    byte[] b = byteArrayBitmapStream.toByteArray();
    encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
    return encodedImage;
}

在我的日志猫下面:

03-22 23:03:44.916 18331-18331/? I/art: Not late-enabling -Xcheck:jni (already on)
03-22 23:03:44.916 18331-18331/? W/art: Unexpected CPU variant for X86 using defaults: x86
03-22 23:03:45.029 18331-18331/com.example.reynaldo.getimageserver W/System: ClassLoader referenced unknown path: /data/app/com.example.reynaldo.getimageserver-2/lib/x86
03-22 23:03:45.038 18331-18331/com.example.reynaldo.getimageserver I/InstantRun: Instant Run Runtime started. Android package is com.example.reynaldo.getimageserver, real application class is null.
03-22 23:03:45.124 18331-18331/com.example.reynaldo.getimageserver W/System: ClassLoader referenced unknown path: /data/app/com.example.reynaldo.getimageserver-2/lib/x86
03-22 23:03:45.372 18331-18331/com.example.reynaldo.getimageserver W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-22 23:03:45.676 18331-18361/com.example.reynaldo.getimageserver D/NetworkSecurityConfig: No Network Security Config specified, using platform default
03-22 23:03:45.865 18331-18367/com.example.reynaldo.getimageserver I/OpenGLRenderer: Initialized EGL, version 1.4
03-22 23:03:45.865 18331-18367/com.example.reynaldo.getimageserver D/OpenGLRenderer: Swap behavior 1
03-22 23:03:45.908 18331-18367/com.example.reynaldo.getimageserver E/EGL_emulation: tid 18367: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
03-22 23:03:45.909 18331-18367/com.example.reynaldo.getimageserver W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb493fe00, error=EGL_BAD_MATCH

【问题讨论】:

  • I'm getting a null pointer exception on the line 你的意思是bMap 是空的吗?
  • 我相信是的,我认为我使用的文件路径不正确。但是我从终端得到了文件路径,放到方法里面还是不行。
  • 可能文件太大了。检查这个答案:stackoverflow.com/a/8442683/5241603
  • 我已将日志猫添加到帖子中
  • @Geek96 是的,一定要确定路径,因为如果指定的文件名是空的,或者不能解码成位图,函数返回空,检查docs。使用try catch 并打印日志

标签: android image encoding bitmap


【解决方案1】:

试试这个

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); // your pic2
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();
    encodedImage = Base64.encodeToString(bitmapdata, Base64.DEFAULT);
    return encodedImage;

【讨论】:

    【解决方案2】:
    Bitmap bitmap = optimizePhoto();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    Strng encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
    

    如果您需要进一步优化图像

    private Bitmap optimizePhoto() {
            // Get the dimensions of the View
            int targetW = 300;
            int targetH = 300;
    
            // Get the dimensions of the bitmap
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(getArguments().getString("imagePath"), bmOptions);
            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;
    
            // Determine how much to scale down the image
            int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
    
            // Decode the image file into a Bitmap sized to fill the View
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            bmOptions.inPurgeable = true;
    
            Bitmap bitmap = BitmapFactory.decodeFile(getArguments().getString("imagePath"), bmOptions);
    
            return bitmap;
        }
    

    【讨论】:

      【解决方案3】:

      你应该试试这个:

       private String convertToBitmap (String name){
                  File imgFile = new File (name);
                  Bitmap bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                   if (bMap != null) {
                           bMap = Bitmap.createScaledBitmap(bMap, 256, 256, true);
                  String encodedImage;
                  ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
                 bm.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayBitmapStream);
                  byte[] b = byteArrayBitmapStream.toByteArray();
                  encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
                  StringBuilder sb = new StringBuilder();
      
                  sb.append(encodedImage);
                  encodeString = sb.toString();
                  return encodedImage;
              }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-17
        • 1970-01-01
        相关资源
        最近更新 更多