val uri = Uri.parse("tel:10086")
val intent = Intent(Intent.ACTION_DIAL,uri)
startActivity(intent)
val smsUri = Uri.parse("smsto:10086")
val intent1 = Intent(Intent.ACTION_SENDTO,smsUri)
intent1.putExtra("sms_body","Hello")
startActivity(intent1)
val webViewUri = Uri.parse("https://www.baidu.com")
val intent2 = Intent(Intent.ACTION_VIEW,webViewUri)
startActivity(intent2)
// /storage/emulated/0/Music/foo.mp3
val file = Environment.getExternalStoragePublicDirectory("Music/foo.mp3")
Log.d(Companion.TAG,file.path)
Log.d(Companion.TAG,file.name)
val intent = Intent(Intent.ACTION_VIEW)
val audio = FileProvider.getUriForFile(this, "$packageName.fileProvider",file)
Log.d(Companion.TAG,audio.path.toString())
intent.setDataAndType(audio,"audio/mp3")
startActivity(intent)
高版本安卓无法直接获取file,需要使用ContentProvide建立临时文件路径
Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Music/foo.mp3 exposed beyond app through Intent.getData()
使用ContentProvide 建立临时文件路径的方法:
1:在Manifest.xml里声明provider
2:res/xml 新建provider_paths.xml文件,内容如下
注意name的值
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains
//打开相机
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
//务必传值,不然onActivityResult照片返回值data为null
intent.putExtra(MediaStore.EXTRA_OUTPUT,savePhoto())
val launcherActivityInfo = registerForActivityResult(ActivityResultContracts.
StartActivityForResult()
) {
if (it != null) {
if (intent.clipData!=null) {
for (i in 0 until intent.clipData!!.itemCount) {
val uri = intent.clipData!!.getItemAt(i).uri
Log.d(TAG, "multiple current Uri:$uri")
}
}
}
}
launcherActivityInfo.launch(intent)
private fun savePhoto():Uri{
val outPath = Environment.getExternalStorageDirectory()
.absoluteFile.absolutePath+"/poo.jpg"
val file = File(outPath)
val audio = FileProvider.getUriForFile(this, "$packageName.fileProvider",file)
Log.d(TAG,audio.path.toString())
return audio
}
// 获取相册并裁剪图片
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
intent.putExtra("crop","true")
intent.putExtra("aspectX",1)
intent.putExtra("aspectY",2)
intent.putExtra("outputX",20)
intent.putExtra("outputY",40)
intent.putExtra("output",savePhoto())
intent.putExtra("outputFormat","JPEG")
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it != null) {
if(it.data!=null){
Log.d(TAG,it.data!!.data.toString())
Glide.with(this).load(it.data!!.data).into(iamge)
}
}
}
launcher.launch(intent)
//进入系统设置页面
val intent = Intent(android.provider.Settings.ACTION_SETTINGS)
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
}
launcher.launch(intent)
//打开系统无线网络设置页面
val intent = Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
}
launcher.launch(intent)