我正在创建一个离线SMS应用程序。我想知道如何将用户选择的图像转换为字符串base64并进行压缩。
我搜索了很多东西,但没有找到太多材料,我发现的所有数据都在Java中。但是我需要Kotlin语言。
活动文件
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)
}
}
期望
将图像转换为base64并进行压缩。
实际
什么都没发生。
答案 0 :(得分:1)
如果由于需要版本26而出现这种情况。低于该版本将不起作用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val bm = BitmapFactory.decodeFile("/path/to/image.jpg")
val stream = ByteArrayOutputStream()
bm.compress(CompressFormat.JPEG, 70, stream)
val byteFormat = stream.toByteArray()
val imgString = Base64.getEncoder().encodeToString(byteFormat)
}
要检索路径/目标/图像:
val uri = data!!.data
val picturePath = getPath(applicationContext, uri) // Write this line under the uri.
Log.d("Picture Path", picturePath)
这是获取Image路径的功能。
private fun getPath(applicationContext: Context, uri: Uri?): String? {
var result: String? = null
val proj = arrayOf(MediaStore.Images.Media.DATA)
val cursor = applicationContext.getContentResolver().query(uri, proj, null, null, null)
if (cursor != null) {
if (cursor!!.moveToFirst()) {
val column_index = cursor!!.getColumnIndexOrThrow(proj[0])
result = cursor!!.getString(column_index)
}
cursor!!.close()
}
if (result == null) {
result = "Not found"
}
return result
}
答案 1 :(得分:0)
在Kotlin中,要将图像文件转换为Base64,然后对其进行压缩
<form action="/action_page.php" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
您必须在第一行中替换图像路径。