Muhammad Husnain Tahir
Muhammad Husnain Tahir

Reputation: 1039

Image to Base64 not working fine android

Hi all i'm capturing an image from camera and converting it to Base64 String, but it is not providing me right Base64 string, only about 20% of image all i can see using Base64 string, on ActivityResult i can see the complete image in ImageView Look at my code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK && data != null) {

        try {

            Bitmap bp = (Bitmap) data.getExtras().get("data");
            img.setImageBitmap(bp);
            imgStr = bitmapToBase64(bp);

        } catch (Exception e) {
            e.printStackTrace();
            Log.d(LOGTAG.logTag, "Error due to : " + e.getMessage());
        }
    }
}


private String bitmapToBase64(Bitmap bitmap) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    String encodedBitmap = Base64.encodeToString(byteArray, Base64.DEFAULT);

    Log.d(LOGTAG.logTag, "" + encodedBitmap);

    return encodedBitmap;

}

this thing is driving me crazy any guidance would be appreciated.

Upvotes: 0

Views: 447

Answers (1)

Rohit Heera
Rohit Heera

Reputation: 2737

Actually when you capture a image from camera the image is not returned in Activity result you get null there so you try this.


/**
     * This method set the path for the captured image from camera for updating
     * the profile pic
     */
    private Uri getOutputMediaFile() {

        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory(), "."
                + Constants.CONTAINER);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        File mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + System.currentTimeMillis() + ".png");
        Uri uri = null;
        if (mediaFile != null) {
            uri = Uri.fromFile(mediaFile);

        }
        return uri;
    }

    call cameraIntent:-

 Intent intent = new Intent(Constants.CAMERA_INTERNAL_CLASS);
fileUri = getOutputMediaFile();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        /* start activity for result pass intent as argument and request code */
        startActivityForResult(intent, requestCode);

         @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);


        if (resultCode == RESULT_OK) {
           try
           {
           String path = fileUri.getPath();

          BitmapFactory.Options options = new BitmapFactory.Options();

        Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
            img.setImageBitmap(bmp);
        imgStr = bitmapToBase64(bmp);
           }catch(Exception e)
           {}

            }
        } 
    }

Upvotes: 1

Related Questions