I am creating an offline SMS app. I want to know how to convert image, chosen by the user, into string base64 and also compressed it.
I have searched a lot but not much material I found.All the data I found is in Java. But I need in Kotlin language.
Activity File
class MainActivity1 :AppCompatActivity(){
private val requestReceiveSms: Int = 1
private val requestSendSms: Int = 2
private var mMessageRecycler: RecyclerView? = null
private var mMessageAdapter: MessageAdapter? = null
val SELECT_PICTURE = 5
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn1.setOnClickListener {
dispatchGalleryIntent()
}
seupRecycler()
val bundle: Bundle? = intent.extras
bundle?.let {
val NUm = bundle.getString("address")
phone.text = NUm
}
btnSend.setOnClickListener {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this, arrayOf(android.Manifest.permission.SEND_SMS),
requestSendSms
)
} else {
SendSms()
}
}
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.RECEIVE_SMS) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.RECEIVE_SMS),
requestReceiveSms)
}
}
private fun seupRecycler() {
mMessageRecycler = this.reyclerview_message_list as RecyclerView
mMessageAdapter = MessageAdapter(this)
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = RecyclerView.VERTICAL
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
grantResults: IntArray) {
if(requestCode == requestSendSms)SendSms()
}
private fun SendSms() {
val str_address = phone
val str_message = txtMessage.text.toString()
SmsManager.getDefault().sendTextMessage(str_address.toString(),null,str_message,null,null)
Toast.makeText(this,"SMS Sent", Toast.LENGTH_SHORT).show()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == SELECT_PICTURE && resultCode == Activity.RESULT_OK){
try {
val uri = data!!.data
imageView2.setImageURI(uri)
}catch (e : IOException){
e.printStackTrace()
}
}
}
fun dispatchGalleryIntent(){
val intent = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
//intent.type = "image/*"
//intent.action = Intent.ACTION_GET_CONTENT
//startActivityForResult(Intent.createChooser(intent,"SELECT IMAGE"), SELECT_PICTURE)
startActivityForResult(intent,SELECT_PICTURE)
}
}
Expected
Convert image into base64 and compress it.
Actual
Nothing happens.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…