lab5
This commit is contained in:
@@ -1,55 +1,58 @@
|
||||
package com.example.myapplication
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import android.widget.ImageView
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import java.io.File
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.content.ContextCompat
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
private lateinit var etInput: EditText
|
||||
private lateinit var btnSave: Button
|
||||
private lateinit var btnLoad: Button
|
||||
private lateinit var tvResult: TextView
|
||||
private lateinit var btnCamera: Button
|
||||
private lateinit var imageView: ImageView
|
||||
|
||||
private val fileName = "myfile.txt"
|
||||
// Новый контракт
|
||||
private val takePicturePreview = registerForActivityResult(
|
||||
ActivityResultContracts.TakePicturePreview()
|
||||
) { bitmap: Bitmap? ->
|
||||
if (bitmap != null) {
|
||||
imageView.setImageBitmap(bitmap)
|
||||
} else {
|
||||
Toast.makeText(this, "Фото не получено", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
// Запрос разрешения на камеру
|
||||
private val requestCameraPermission = registerForActivityResult(
|
||||
ActivityResultContracts.RequestPermission()
|
||||
) { granted ->
|
||||
if (granted) {
|
||||
takePicturePreview.launch(null)
|
||||
} else {
|
||||
Toast.makeText(this, "Разрешение на камеру не выдано", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
etInput = findViewById(R.id.etInput)
|
||||
btnSave = findViewById(R.id.btnSave)
|
||||
btnLoad = findViewById(R.id.btnLoad)
|
||||
tvResult = findViewById(R.id.tvResult)
|
||||
btnCamera = findViewById(R.id.btnCamera)
|
||||
imageView = findViewById(R.id.imageView)
|
||||
|
||||
btnSave.setOnClickListener { saveToFile() }
|
||||
btnLoad.setOnClickListener { loadFromFile() }
|
||||
}
|
||||
|
||||
private fun saveToFile() {
|
||||
val text = etInput.text.toString()
|
||||
if (text.isEmpty()) {
|
||||
Toast.makeText(this, "Введите текст", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
val file = File(filesDir, fileName)
|
||||
file.writeText(text)
|
||||
|
||||
Toast.makeText(this, "Файл сохранён", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
private fun loadFromFile() {
|
||||
val file = File(filesDir, fileName)
|
||||
if (file.exists()) {
|
||||
val content = file.readText()
|
||||
tvResult.text = "Содержимое файла: $content"
|
||||
} else {
|
||||
tvResult.text = "Файл не найден"
|
||||
btnCamera.setOnClickListener {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||
== PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
takePicturePreview.launch(null)
|
||||
} else {
|
||||
requestCameraPermission.launch(Manifest.permission.CAMERA)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user