0

The user clicks image from his phone (using camera 2 api). I am storing it on users device using following function. I am not using bitmap.

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // create the file where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException e) {
            // Error message
            Toast.makeText(this, "Error occured while creating the file", Toast.LENGTH_SHORT).show();
        }

        //Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this, "com.valuefintech.android.fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
        }
    }
}

private File createImageFile() throws IOException {
    String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timestamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,
            ".jpg",
            storageDir
    );

    //save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    return image;
}

and then it displays the image on imageview using following code

if(requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

            File f = new File(currentPhotoPath);

            idImage.setImageURI(Uri.fromFile(f));
            Log.d("tag", "Absolute url of image is " + Uri.fromFile(f));

        byte[] b = new byte[(int) f.length()];
        try {
            FileInputStream fileInputStream = new FileInputStream(f);
            fileInputStream.read(b);
            for (int j = 0; j < b.length; j++) {
                System.out.print((char) b[j]);
            }
        } catch (FileNotFoundException e) {
            System.out.println("File Not Found.");
            e.printStackTrace();
        } catch (IOException e1) {
            System.out.println("Error Reading The File.");
            e1.printStackTrace();
        }

        byte[] byteFileArray = new byte[0];
        try {
            byteFileArray = FileUtils.readFileToByteArray(f);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String base64String = "";
        if (byteFileArray.length > 0) {
            base64String = android.util.Base64.encodeToString(byteFileArray, android.util.Base64.NO_WRAP);
            Log.i("File Base64 string", "IMAGE PARSE ==>" + base64String);
        }

            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri contentUri = Uri.fromFile(f);
            mediaScanIntent.setData(contentUri);
            this.sendBroadcast(mediaScanIntent);

    }

The problem is I am not able to convert the image into Base64 so that I can upload it to my server.

  • does _Log.i("File Base64 string", "IMAGE PARSE ==>" + base64String);_ print anything? are you getting anything in logs? – kelvin Apr 19 '21 at 12:04
  • actually it doesn't even run,on the part where file should be coverted to bytearray my IDE show Cannot resolve method 'readFileToByteArray' in 'FileUtils' – Neel Patel Apr 19 '21 at 12:07
  • https://stackoverflow.com/questions/36492084/how-to-convert-an-image-to-base64-string-in-java Please check out this answer – kelvin Apr 19 '21 at 12:12
  • @kelvin I tried the method shown above before this but my IDE shows "cannot resolve method'encodeBase64' in 'Base64'. – Neel Patel Apr 19 '21 at 12:19

0 Answers0