320x100
[상황]
버튼 클릭(이벤트 발생) 시
특정 view 를 디바이스(모바일) 다운로드 폴더에
저장하고 싶을 경우가 발생
[참고 플랫폼]
더보기
1. Android Studio Bumblebee | 2021.1.1 Patch 2 | Runtime version: 11.0.11
[코드 1 : MainActivity.kt]
class MainActivity : AppCompatActivity() {
private lateinit var targetView: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
targetView = findViewById(R.id.lL_target)
findViewById<Button>(R.id.btnSave).setOnClickListener {
viewSave(targetView)
}
}
private fun getViewBitmap(view: View): Bitmap {
val bitmap = Bitmap.createBitmap(
view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
view.draw(canvas)
return bitmap
}
private fun getSaveFilePathName(): String {
val folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()
val fileName = SimpleDateFormat("yyyyMMddHHmmss").format(Date())
return "$folder/$fileName.jpg"
}
private fun bitmapFileSave(bitmap: Bitmap, path: String) {
val fos: FileOutputStream
try{
fos = FileOutputStream(File(path))
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos)
fos.close()
}catch (e: IOException) {
e.printStackTrace()
}
}
private fun viewSave(view: View) {
val bitmap = getViewBitmap(view)
val filePath = getSaveFilePathName()
bitmapFileSave(bitmap, filePath)
}
}
[코드 2 : 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">
<LinearLayout
android:id="@+id/lL_target"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/gray"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/ic_launcher_foreground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="나에게 안드로이드란?" />
</LinearLayout>
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="viewSave"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
[코드 : 적용 결과]
[코드 : 사진 결과]
320x100
'Android > Kotlin' 카테고리의 다른 글
[Android][Kotlin] Dialog 커스텀 및 스타일 수정 (0) | 2022.10.05 |
---|---|
[Android][Kotlin] 특정 시간 뒤 작업 Handler postDelayed (0) | 2022.10.04 |
[Android][Kotlin] AlertDialog (2) | 2022.10.04 |
[Android][Kotlin] View 테두리 코드로 변경하는 방법 (0) | 2022.09.23 |
[Android][Kotlin] 메서드 파라미터 함수 넣기 : 고차 함수 활용 (0) | 2022.09.20 |