lab5
This commit is contained in:
@@ -2,7 +2,8 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
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
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|||||||
@@ -1,55 +1,58 @@
|
|||||||
package com.example.myapplication
|
package com.example.myapplication
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.graphics.Bitmap
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
import android.widget.EditText
|
import android.widget.ImageView
|
||||||
import android.widget.TextView
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import java.io.File
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
|
||||||
private lateinit var etInput: EditText
|
private lateinit var btnCamera: Button
|
||||||
private lateinit var btnSave: Button
|
private lateinit var imageView: ImageView
|
||||||
private lateinit var btnLoad: Button
|
|
||||||
private lateinit var tvResult: TextView
|
|
||||||
|
|
||||||
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?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
etInput = findViewById(R.id.etInput)
|
btnCamera = findViewById(R.id.btnCamera)
|
||||||
btnSave = findViewById(R.id.btnSave)
|
imageView = findViewById(R.id.imageView)
|
||||||
btnLoad = findViewById(R.id.btnLoad)
|
|
||||||
tvResult = findViewById(R.id.tvResult)
|
|
||||||
|
|
||||||
btnSave.setOnClickListener { saveToFile() }
|
btnCamera.setOnClickListener {
|
||||||
btnLoad.setOnClickListener { loadFromFile() }
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||||
}
|
== PackageManager.PERMISSION_GRANTED
|
||||||
|
) {
|
||||||
private fun saveToFile() {
|
takePicturePreview.launch(null)
|
||||||
val text = etInput.text.toString()
|
} else {
|
||||||
if (text.isEmpty()) {
|
requestCameraPermission.launch(Manifest.permission.CAMERA)
|
||||||
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 = "Файл не найден"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,39 +4,17 @@
|
|||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:padding="16dp">
|
android:padding="16dp">
|
||||||
|
|
||||||
<EditText
|
<Button
|
||||||
android:id="@+id/etInput"
|
android:id="@+id/btnCamera"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:hint="Введите текст"
|
android:text="Сделать фото" />
|
||||||
android:inputType="text" />
|
|
||||||
|
|
||||||
<LinearLayout
|
<ImageView
|
||||||
|
android:id="@+id/imageView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="0dp"
|
||||||
android:orientation="horizontal"
|
android:layout_weight="1"
|
||||||
android:layout_marginTop="16dp">
|
android:layout_marginTop="16dp"
|
||||||
|
android:scaleType="centerCrop" />
|
||||||
<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" />
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
Reference in New Issue
Block a user