lab5
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
<uses-feature android:name="android.hardware.camera" android:required="false" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
||||
@@ -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"
|
||||
btnCamera.setOnClickListener {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||
== PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
takePicturePreview.launch(null)
|
||||
} else {
|
||||
tvResult.text = "Файл не найден"
|
||||
requestCameraPermission.launch(Manifest.permission.CAMERA)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,39 +4,17 @@
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etInput"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Введите текст"
|
||||
android:inputType="text" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:layout_marginTop="16dp">
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnSave"
|
||||
android:layout_width="0dp"
|
||||
android:id="@+id/btnCamera"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Сохранить" />
|
||||
android:text="Сделать фото" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnLoad"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Загрузить" />
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvResult"
|
||||
<ImageView
|
||||
android:id="@+id/imageView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="24dp"
|
||||
android:text="Здесь появится содержимое файла"
|
||||
android:textSize="18sp" />
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_marginTop="16dp"
|
||||
android:scaleType="centerCrop" />
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user