**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)
}
}
}
}
}