Android Application
Kotlin 파일처리 : 권한과 디렉토리 확인하기
작성자 임베디드코리아
작성일 23-10-06 22:13
조회1,529회
댓글0건
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/snackbar_button"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="권한 확인"
tools:layout_editor_absoluteX="5dp"
tools:layout_editor_absoluteY="20dp" />
<TextView
android:id="@+id/tvDirectory1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="70dp"
android:layout_marginBottom="10dp"
android:text="TextView1"
android:background="#9BF8ED"
app:layout_constraintBottom_toTopOf="@+id/tvDirectory2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvDirectory2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="TextView2"
android:background="#9BF8ED"
app:layout_constraintBottom_toTopOf="@+id/tvDirectory3"
app:layout_constraintTop_toBottomOf="@+id/tvDirectory1"
/>
<TextView
android:id="@+id/tvDirectory3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="TextView3"
android:background="#9BF8ED"
app:layout_constraintTop_toBottomOf="@+id/tvDirectory2"
app:layout_constraintBottom_toTopOf="@+id/tvDirectory4"
/>
<TextView
android:id="@+id/tvDirectory4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="TextView4"
android:background="#9BF8ED"
app:layout_constraintTop_toBottomOf="@+id/tvDirectory3"
app:layout_constraintBottom_toTopOf="@+id/tvDirectory5"
/>
<TextView
android:id="@+id/tvDirectory5"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="TextView5"
android:background="#9BF8ED"
app:layout_constraintTop_toBottomOf="@+id/tvDirectory4"
app:layout_constraintBottom_toTopOf="@+id/tvDirectory6" />
<TextView
android:id="@+id/tvDirectory6"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="TextView6"
android:background="#9BF8ED"
app:layout_constraintTop_toBottomOf="@+id/tvDirectory5"
app:layout_constraintBottom_toTopOf="@+id/tvDirectory7" />
<TextView
android:id="@+id/tvDirectory7"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="TextView7"
android:background="#9BF8ED"
app:layout_constraintTop_toBottomOf="@+id/tvDirectory6"
app:layout_constraintBottom_toTopOf="@+id/tvDirectory8" />
<TextView
android:id="@+id/tvDirectory8"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="TextView8"
android:background="#9BF8ED"
app:layout_constraintTop_toBottomOf="@+id/tvDirectory7" />
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var tvDirectory1: TextView
private lateinit var tvDirectory2: TextView
private lateinit var tvDirectory3: TextView
private lateinit var tvDirectory4: TextView
private lateinit var tvDirectory5: TextView
private lateinit var tvDirectory6: TextView
private lateinit var tvDirectory7: TextView
private lateinit var tvDirectory8: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvDirectory1 = findViewById(R.id.tvDirectory1)
tvDirectory2 = findViewById(R.id.tvDirectory2)
tvDirectory3 = findViewById(R.id.tvDirectory3)
tvDirectory4 = findViewById(R.id.tvDirectory4)
tvDirectory5 = findViewById(R.id.tvDirectory5)
tvDirectory6 = findViewById(R.id.tvDirectory6)
tvDirectory7= findViewById(R.id.tvDirectory7)
tvDirectory8 = findViewById(R.id.tvDirectory8)
var button = findViewById(R.id.snackbar_button) as Button
button.setOnClickListener {
checkPermission()
}
/* 내부저장소 */
// 캐시(Cache)
val fileCacheDir = cacheDir
val getCacheDir = fileCacheDir.path
tvDirectory1.setText(getCacheDir)
// 데이터베이스(Database)
val fileDataBase = getDatabasePath("Android")
val getDatabasePath = fileDataBase.path
tvDirectory2.setText(getDatabasePath)
// 일반 파일
// 일반 파일
val fileFile = filesDir
val getFile = fileFile.path
tvDirectory3.setText(getFile)
// 일반 파일 폴더
// 일반 파일 폴더
val fileFileName = getFileStreamPath("Android")
val getFileName = fileFileName.path
tvDirectory4.setText(getFileName)
/* 외부저장소 - 공용 영역 */
// 최상위 경로
val getDirectory = Environment.getExternalStorageDirectory().toString() + "/tmp"
tvDirectory5.setText(getDirectory)
// 특정 데이터를 저장
val fileAlarms = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS)
val getAlarms = fileAlarms.path
tvDirectory6.setText(getAlarms)
/* 외부저장소 - 어플리케이션 고유 영역 */
// 특정 데이터를 저장
val fileAlarms2 = getExternalFilesDirs(Environment.DIRECTORY_ALARMS)
val getAlarms2 = fileAlarms2[0].path
tvDirectory7.setText(getAlarms2)
// 캐시 데이터를 저장
val getCache2 = externalCacheDir.toString() + "/tmp"
tvDirectory8.setText(getCache2)
}
//권한 확인
fun checkPermission() {
// 1. 위험권한(Camera) 권한 승인상태 가져오기
val cameraPermission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
if (cameraPermission == PackageManager.PERMISSION_GRANTED) {
// 카메라 권한이 승인된 상태일 경우
startProcess()
} else {
// 카메라 권한이 승인되지 않았을 경우
requestPermission()
}
}
// 2. 권한 요청
private fun requestPermission() {
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CAMERA), 99)
}
// 권한 처리
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray) {
when (requestCode) {
99 -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startProcess()
} else {
Log.d("MainActivity", "종료")
}
}
}
}
// 3. 카메라 기능 실행
fun startProcess() {
Toast.makeText(this, "카메라 기능 실행", Toast.LENGTH_SHORT).show()
}
}
------------------------------------------------------------------------------------
*** AndroidManifest.xml
------------------------------------------------------------------------------------
<!-- 권한 그룹 : CAMERA -->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- 권한 그룹 : STORAGE-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.