0

I'm trying to convert an image to a bitmap and then encode it in a base64 string.I'm getting a null pointer exception on the line

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

what is wrong? Note that I am inputing the file name with the picture "pic2.jpg" to the method below.

below is my code:

    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;
}

below my log cat:

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
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
Geek96
  • 57
  • 8

3 Answers3

0

Try this

    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;
John Joe
  • 12,412
  • 16
  • 70
  • 135
0
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);

If you need to optimize image furthur

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;
    }
taman neupane
  • 938
  • 9
  • 18
0

You should try this:

 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;
        }
}
Ghanshyam Sharma
  • 451
  • 9
  • 21