Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Hook
- 랜덤넘버
- fragment
- JavaScript
- nodejs
- Java
- TypeScript
- Android
- Javscript
- 자바스크립트
- Kotlin
- scrollview
- GoogleMaps
- TextView
- 코틀린
- 구글맵스
- 리액트
- SpringBoot
- npm
- RecyclerView
- 오버라이딩
- stylesheet
- button
- 스프링부트
- array
- JS
- React
- 안드로이드
- Linux
- 랜덤번호
Archives
- Today
- Total
타닥타닥 개발자의 일상
Kotlin 코틀린 저장된 사진 불러와서 안드로이드 화면에 구현하기 / File 본문
편집해야할 폴더 및 파일 구성
MainActivity,
activity_main.xml,
AndroidManifest.xml
AndroidManifest.xml
저장소의 파일을 읽고 불러올수 있도록 아래의 코드를 삽입해준다.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
코드가 적용된 전체 문서
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imagecall">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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.ImageCall"
android:requestLegacyExternalStorage="true"
>
<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>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="333dp"
android:layout_height="317dp" tools:srcCompat="@tools:sample/avatars" android:id="@+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintVertical_bias="0.458"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.imagecall
import android.Manifest
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import java.io.File
class MainActivity : AppCompatActivity() {
lateinit var locationPermission: ActivityResultLauncher<Array<String>>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
locationPermission = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()){ results ->
if(!results.all{it.value}){ //만약 괄호 안 값이 false였을떄
Toast.makeText(this,"권한 승인이 필요합니다.",Toast.LENGTH_LONG).show()
}
}
//권한 허가를 내준다.
locationPermission.launch(
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
)
val imageView = findViewById<ImageView>(R.id.imageView)
//불러오고자 하는 경로와 파일명(파일명:20220216111140.jpg)
val file:File = File("/storage/emulated/0/Pictures/20220216111140.jpg")
val fExist = file.exists() //파일이 있는지 없는지에 대한 true 또는 false가 넘어온다.
if(fExist){
Log.d("","이미지 파일이 존재합니다.")
/*실제 화면에 보여지는 파일은 BitmaptFactory와 연결된 파일이다.
File()안에 입력한 파일과 달라도 화면에는 문제없이 출력된다.*/
val myBitmap = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/20220217092453.jpg")
imageView.setImageBitmap(myBitmap)
}else{
Log.d("","이미지 파일이 존재하지 않습니다.")
}
}
}
실행화면
'코딩 기록 > Kotlin' 카테고리의 다른 글
Comments