Select file from internal storage – Android kotlin
To select a file from internal storage in android using Kotlin, first, you have to start an intent with a request code.
val intent = Intent(Intent.ACTION_PICK)
intent.type = "*/*"
startActivityForResult(intent, 565)
After that, when the image is selected from the gallery by the user, it will be received in the onActivityResult method. So we need to create the following methods in our activity:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == 565) {
val uri: Uri? = data?.data
var base64: String = ""
try {
val bytes = uri?.let { contentResolver.openInputStream(it)?.readBytes() }
base64 = Base64.encodeToString(bytes, Base64.URL_SAFE)
} catch (e1: IOException) {
e1.printStackTrace()
}
var attachmentName: String = uri?.let { getFileName(it, contentResolver) }.toString()
var extension: String = uri?.let {
getMimeType(
applicationContext,
it
)
}.toString()
Log.i("mylog, base64 = ", base64)
Log.i("mylog, attachment = ", attachmentName)
Log.i("mylog, extension = ", extension)
}
}
fun getFileName(uri: Uri, contentResolver: ContentResolver): String {
val returnCursor: Cursor = contentResolver.query(uri, null, null, null, null)!!
val nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
returnCursor.moveToFirst()
val name = returnCursor.getString(nameIndex)
returnCursor.close()
return name
}
fun getMimeType(context: Context, uri: Uri): String? {
return if (uri.scheme == ContentResolver.SCHEME_CONTENT) {
//If scheme is a content
val mime = MimeTypeMap.getSingleton()
mime.getExtensionFromMimeType(context.contentResolver.getType(uri))
} else {
//If scheme is a File
//This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(File(uri.path)).toString())
}
}
Run the app now, you will be able to select any file from your internal or external storage (if you have external storage). After you select the file, you will see its base64 content, file name, and its extension in the logcat of android studio.