0

**I want to convert image into base64 string. Please help me with my code in android studio

This is my code and help me with it in kotlin android I tried so many ways but i can't get it can you guys help me with that. i also want to know that in which function i need to convert the image into base64 and where the image is storing and i am getting the image directly from the gallery

**

    private var uploadImageLabelText: TextView? = null
    private var uploadImageBox: RelativeLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_support)
    setStatusBarColorLight(R.color.dim_white)

      uploadImageLabelText = getView(R.id.upload_image_label)
      uploadImageBox = getView(R.id.upload_image_box)

      uploadImageBox?.setOnClickListener {
            requestExternalReadPermission()


       private fun requestExternalReadPermission() {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            )
            != PackageManager.PERMISSION_GRANTED
            ) {

            ActivityCompat.requestPermissions(
                this,
                arrayOf<String> 
            (Manifest.permission.READ_EXTERNAL_STORAGE),
                FG_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE
            );
        } else {
            chooseFile()
        }
        }
        private fun chooseFile() {
        var chooseFile = Intent(Intent.ACTION_GET_CONTENT)
        chooseFile.type = "*/*"
        chooseFile = Intent.createChooser(chooseFile, "Select a file to 
       upload the image")
        startActivityForResult(chooseFile, 
        SUPPORT_SCREEN_CHOOSE_UPLOAD_FILE_REQUEST_CODE)
        }
      override fun onActivityResult(requestCode: Int, resultCode: Int, 
         data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

     if (requestCode == SUPPORT_SCREEN_CHOOSE_UPLOAD_FILE_REQUEST_CODE && 
      resultCode == RESULT_OK
    ) {
        data?.data?.let { uri ->

            uploadFileUri = uri

            val cursor = contentResolver.query(uri, null, null, null,  
            null) 
        
            if (cursor != null) {
                val nameIndex = 
                 cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
                val sizeIndex = 
                cursor.getColumnIndex(OpenableColumns.SIZE)
                cursor.moveToFirst()


                val fileName = cursor.getString(nameIndex)
                System.out.println(fileName)
                setFileNameToUploadButton(fileName)

                //saveFileToCache(cursor.getString(nameIndex))

            } else {

                val filePath = uri.path

                var fileName = ""

                val cut = filePath.lastIndexOf('/')

                if (cut != -1) {
                    fileName = filePath.substring(cut + 1)
                }


                setFileNameToUploadButton(fileName)
                System.out.println(fileName)
                //saveFileToCache(fileName)
            }
        }
    }

}


            
        }
  • 1
    [Choose an image from gallery and store in file](https://stackoverflow.com/questions/23074845/android-choose-image-from-gallery-and-store-it-in-a-file-type-variable) – mightyWOZ Feb 05 '21 at 09:16
  • 1
    [How to convert a file to base64](https://stackoverflow.com/questions/28758014/how-to-convert-a-file-to-base64) – mightyWOZ Feb 05 '21 at 09:16
  • 1
    Does this answer your question? [How to convert a file to Base64?](https://stackoverflow.com/questions/28758014/how-to-convert-a-file-to-base64) – mightyWOZ Feb 05 '21 at 09:17

1 Answers1

0

In your onActivityResult() you can use such way:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            val documentFile: DocumentFile = DocumentFile.fromSingleUri(this, data!!.data!!)!!
            val string = data.data?.let { uri -> contentResolver.openInputStream(uri).use { it!!.readBytes() } }


            //Base64.encodeToString(string, Base64.NO_WRAP)

        }
    }

here:

Base64.encodeToString(string, Base64.NO_WRAP)

you can get your file encoded to Base64 string and process it. I hope that it will help you :)

Andrew
  • 1,947
  • 2
  • 23
  • 61