Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val registerLauncher =
- registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { ActivityResult ->
- if (ActivityResult.resultCode == Activity.RESULT_OK) {
- //當launcher 成功你所要執行的動作
- getIntentData(ActivityResult.data)
- }
- }
- //點選Image View顯示file picker,選取讀片
- findViewById<ImageView>(R.id.img).setOnClickListener {
- Intent(Intent.ACTION_GET_CONTENT).also {
- it.type = "image/*"
- //底下是實際觸發launch
- registerLauncher.launch(it)
- }
- }
- }
- private fun getIntentData(data: Intent?) {
- data?.data?.let {
- curFile = it
- findViewById<ImageView>(R.id.img).setImageURI(it)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement