Skip to content

Commit

Permalink
Improved the Photo Picker API for image compressor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai-Cristian Condrea committed Oct 5, 2024
1 parent ad5dd97 commit aeac1a0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId = "com.d4rk.cleaner"
minSdk = 23
targetSdk = 34
versionCode = 131
versionName = "2.0.0"
versionCode = 132
versionName = "3.0.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
resourceConfigurations += listOf(
"en",
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
android:resource="@xml/provider_paths" />
</provider>


<!--suppress AndroidDomInspection -->
<service
android:name="com.google.android.gms.metadata.ModuleDependencies"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,62 @@ package com.d4rk.cleaner.ui.screens.imageoptimizer.imagepicker
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.d4rk.cleaner.R
import com.d4rk.cleaner.ui.screens.settings.display.theme.style.AppTheme

class ImagePickerActivity : AppCompatActivity() {
private val viewModel: ImagePickerViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
private val viewModel : ImagePickerViewModel by viewModels()

private val pickMediaLauncher =
registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
viewModel.setSelectedImageUri(uri)
}

private val openDocumentLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK && result.data != null) {
val selectedImageUri : Uri? = result.data?.data
viewModel.setSelectedImageUri(selectedImageUri)
}
}

override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
AppTheme {
Surface(
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
modifier = Modifier.fillMaxSize() , color = MaterialTheme.colorScheme.background
) {
ImagePickerComposable(activity = this@ImagePickerActivity, viewModel)
ImagePickerComposable(activity = this@ImagePickerActivity , viewModel)
}
}
}
}

private val launcher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK && result.data != null) {
val selectedImageUri: Uri? = result.data?.data
viewModel.setSelectedImageUri(selectedImageUri)
}
fun selectImage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
pickMediaLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
}
else {
val pickIntent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
type = "image/*"
addCategory(Intent.CATEGORY_OPENABLE)
}

fun selectImage() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
val chooser: Intent = Intent.createChooser(intent, getString(R.string.select_image))
launcher.launch(chooser)
openDocumentLauncher.launch(pickIntent)

}
}
}

0 comments on commit aeac1a0

Please sign in to comment.