This commit is contained in:
Ваше Имя
2025-09-24 14:29:16 +04:00
parent b8d120454d
commit 7b78d9bfc2
3 changed files with 51 additions and 69 deletions
+2 -1
View File
@@ -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"
} else {
tvResult.text = "Файл не найден"
btnCamera.setOnClickListener {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED
) {
takePicturePreview.launch(null)
} else {
requestCameraPermission.launch(Manifest.permission.CAMERA)
}
}
}
}
+10 -32
View File
@@ -4,39 +4,17 @@
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/etInput"
android:layout_width="match_parent"
<Button
android:id="@+id/btnCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Введите текст"
android:inputType="text" />
android:text="Сделать фото" />
<LinearLayout
<ImageView
android:id="@+id/imageView"
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:layout_height="wrap_content"
android:layout_weight="1"
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"
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>