์ฃผ์๋ก ์ฑ์ ์ฐ๋ํ์ฌ ์ฃผ์๋ก ๋ชฉ๋ก ํ๋ฉด (์กํฐ๋นํฐ)์ ๋์ฐ๊ณ , ์ ์ ๊ฐ ์ ํํ ์ฌ๋์ ์ ํ๋ฒํธ ํน์ ์ด๋ฉ์ผ ์ ๋ณด๋ฅผ ์ป์ด๋ณด์! (์ปจํ ์ธ ํ๋ก๋ฐ์ด๋)
<uses-permission android:name="android.permission.READ_CONTACTS"/>
val intent = Intent(Intent.ACTION_PICK, ContentsContract.CommonDataKinds.Phone.CONTENT_URI)
requestActivity(intent) // ์์์ ์ธํ
ํธ ์ ๋ฌ
<uses-permission android:name="android.permission.READ_CONTACTS"/>
package com.tutorial.c63
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.ContactsContract
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val resultView = findViewById<TextView>(R.id.resultView)
// ๋ค๋ฅธ ์กํฐ๋นํฐ๋ก๋ถํฐ ๊ฒฐ๊ณผ ๊ฐ์ ๋ฐ๋ ๊ฒฝ์ฐ -> StartActivityForResult
val requestActivity = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
// ์ปจํ
์ธ ํ๋กํ์ด๋์๊ฒ ๋ฐ์ดํฐ ์์ฒญ
val cursor = contentResolver.query(
it.data!!.data!!, // ์ ์ ๊ฐ ์ ํํ ํญ๋ชฉ์ ์๋ณ์ ๊ฐ (uri)
arrayOf(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, // ์ด๋ฆ
ContactsContract.CommonDataKinds.Phone.NUMBER // ์ ํ๋ฒํธ
),
null,
null,
null
)
var name = "none"
var phone = "none"
if (cursor!!.moveToFirst()) {
name = cursor.getString(0)
phone = cursor.getString(1)
}
// ์ด๋ฆ๊ณผ ์ ํ๋ฒํธ๋ฅผ ํ
์คํธ๋ทฐ์ ๋ณด์ฌ์ฃผ๊ธฐ
resultView.text = "result: name - ${name}, phone - ${phone}"
}
// ์ฃผ์๋ก์ ๋ํ ํผ๋ฏธ์
์์ฒญ
val permissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) { // ํผ๋ฏธ์
์ด ํ์ฉ๋๋ฉด
// ์ฃผ์๋ก ๋ชฉ๋ก ํ๋ฉด์ผ๋ก ๋์ด๊ฐ๊ธฐ
val intent = Intent(
Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI // ์ ํ๋ฒํธ๊ฐ ์๋ ์ฌ๋๋ง ์ถ๋ ฅ
)
requestActivity.launch(intent)
} else {
Toast.makeText(this, "permission denied", Toast.LENGTH_SHORT).show()
}
}
button.setOnClickListener {
// ์ฃผ์๋ก ํผ๋ฏธ์
ํ์ฉ ์ฌ๋ถ ํ์ธ
val status = ContextCompat.checkSelfPermission(this,
"android.permission.READ_CONTACTS")
if(status == PackageManager.PERMISSION_GRANTED){
// ์ฃผ์๋ก ๋ชฉ๋ก ํ๋ฉด์ผ๋ก ๋์ด๊ฐ๊ธฐ
val intent = Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI)
requestActivity.launch(intent)
}else{
permissionLauncher.launch("android.permission.READ_CONTACTS")
}
}
}
}
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.type = "image/*"
requestActivity.launch(intent)
val option = BitmapFactory.Options()
option.inSampleSize = 5
var inputStream = contentResolver.openInputStream(it.data!!.data!!)
val bitmap = BitmapFactory.decodeStream(inputStream, null, option)
package com.tutorial.c64
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageView = findViewById<ImageView>(R.id.image_view)
val button = findViewById<Button>(R.id.button)
val launcher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
try {
val option = BitmapFactory.Options()
option.inSampleSize = 5
// ์ปจํ
์ธ ํ๋ก๋ฐ์ด๋์๊ฒ ์ ๊ณต๋ฐ์ ์๋ณ์ ๊ฐ์ผ๋ก ๋นํธ๋งต ๊ฐ์ฒด ์์ฑ
val inputStream = contentResolver.openInputStream(it.data!!.data!!)
val bitmap = BitmapFactory.decodeStream(inputStream, null, option)
inputStream!!.close()
bitmap?.let {
imageView.setImageBitmap(bitmap) // ์ด๋ฏธ์ง ์ค์
} ?: let {
Toast.makeText(this, "์ด๋ฏธ์ง ๋ก๋ฉ ์คํจ", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
button.setOnClickListener {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.type = "image/*"
launcher.launch(intent) // ์์์ ์ธํ
ํธ๋ก ๊ฐค๋ฌ๋ฆฌ ์ฑ ๋์ฐ๊ธฐ
}
}
}
tel:
๋ก ์ ์ธ, ๊ทธ ๋ค์ ์ ํ๋ฒํธ ๋ช
์ <uses-permission android:name="android.permission.CALL_PHONE"/>
package com.tutorial.c65
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editView = findViewById<EditText>(R.id.editView)
val button = findViewById<Button>(R.id.button)
val permissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
){ isGranted ->
if(isGranted){
val intent = Intent(Intent.ACTION_CALL,
Uri.parse("tel:${editView.text}"))
startActivity(intent) // ์์์ ์ธํ
ํธ๋ก ์ ํ ์ฑ ๋์ฐ๊ธฐ
}else{
Toast.makeText(this, "denied", Toast.LENGTH_SHORT).show()
}
}
button.setOnClickListener {
val status = ContextCompat.checkSelfPermission(this,
"android.permission.CALL_PHONE")
if(status == PackageManager.PERMISSION_GRANTED){
val intent = Intent(Intent.ACTION_CALL,
Uri.parse("tel:${editView.text}"))
startActivity(intent)
}else{ // ํผ๋ฏธ์
์ฌ์์ฒญ
permissionLauncher.launch("android.permission.CALL_PHONE")
}
}
}
}
์นด๋ฉ๋ผ ์ฑ์ ์ฐ๋ํ์ฌ ์ฌ์ง์ ์ดฌ์ํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋๋ ค๋ฐ๋ ๋ฐฉ๋ฒ์๋ 2๊ฐ์ง๊ฐ ์๋ค.
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
requestActivity.launch(intent)
val bitmap = it.data?.getExtras()?.get("data") as Bitmap
์ฌ์ง์ ์ ์ฅํ ํ์ผ
์ ๋ง๋ ๋ค. ์นด๋ฉ๋ผ ์ฑ์ ์คํ
ํ๋ค. ๊ณต์ ๋ ํ์ผ์ ์ ์ฅ
ํ๋ค. ์ฑ๊ณต/์คํจ ์ฌ๋ถ
๋ฅผ ๋ฐํํ๋ค. FileProvider
๊ฐ ์ฌ์ฉ๋๋ค.
val bitmap = BitmapFactory.decodeFile(filePath, option)
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="myfiles"
path="Android/data/com.tutorial.c66/files/Pictures"/>
</paths>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.c66">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLab">
<provider
android:authorities="com.tutorial.c66.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"/>
</provider>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="150dp"
app:cardCornerRadius="75dp"
app:cardElevation="5dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</androidx.cardview.widget.CardView>
<Button
android:id="@+id/dataButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="data" />
<Button
android:id="@+id/fileButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="file" />
</LinearLayout>
package com.tutorial.c66
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageView = findViewById<ImageView>(R.id.imageView)
val dataButton = findViewById<Button>(R.id.dataButton)
val fileButton = findViewById<Button>(R.id.fileButton)
val launcher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
){
// ๋ถ๊ฐ ๋ฐ์ดํฐ๋ก๋ถํฐ ๋นํธ๋งต ๊ฐ์ฒด ํ๋
val bitmap = it.data?.extras?.get("data") as Bitmap
bitmap.let{
imageView.setImageBitmap(bitmap)
}
}
dataButton.setOnClickListener {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) // ์ด๋ฏธ์ง ์บก์ณ ์ก์
launcher.launch(intent) // ์์์ ์ธํ
ํธ๋ก ์นด๋ฉ๋ผ ์ฑ ๋์ฐ๊ธฐ
}
var filePath = ""
val fileLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
){
// ์ด๋ฏธ์ง ํ์ผ๋ก๋ถํฐ ๋นํธ๋งต ๊ฐ์ฒด ์์ฑ
val option = BitmapFactory.Options()
option.inSampleSize = 3
val bitmap = BitmapFactory.decodeFile(filePath, option)
bitmap?.let {
imageView.setImageBitmap(bitmap)
}
}
fileButton.setOnClickListener {
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val file = File.createTempFile(
"JPEG_${timeStamp}_",
".jpg",
storageDir
)
filePath = file.absolutePath // ํ์ผ ๊ฒฝ๋ก๋ช
์ค์
val uri = FileProvider.getUriForFile(
this,
"com.tutorial.c66.fileprovider",
file
)
// ์์์ ์ธํ
ํธ์ ๋ถ๊ฐ ๋ฐ์ดํฐ๋ก ํ์ผ uri ๋ฑ๋กํ๊ณ ์นด๋ฉ๋ผ ์ฑ ๋์ฐ๊ธฐ
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
fileLauncher.launch(intent)
}
}
}