Skip to content

Commit

Permalink
update to photopicker for selecting images on feature detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
jclark118 committed Nov 10, 2023
1 parent 884d097 commit 86dcc27
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.7.10'
ext.kotlin_version = '1.8.0'

repositories {
google()
Expand Down
1 change: 1 addition & 0 deletions mapcache/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ task androidAppVersion {
dependencies {
api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
api 'androidx.appcompat:appcompat:1.6.1'
api 'androidx.activity:activity:1.8.0'
api 'com.google.android.material:material:1.6.0'
api 'androidx.preference:preference:1.2.1'
api 'androidx.lifecycle:lifecycle-extensions:2.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
Expand Down Expand Up @@ -156,16 +157,19 @@ public class FeatureViewActivity extends AppCompatActivity {
private DeleteImageListener deleteImageListener;

/**
* result listener for selecting images from the gallery
* Result listener for selecting images from the gallery.
* Registers a photo picker activity launcher in single-select mode.
*/
private ActivityResultLauncher<String> getImageFromGallery = registerForActivityResult(new ActivityResultContracts.GetContent(),
new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri uri) {
ActivityResultLauncher<PickVisualMediaRequest> getImageFromGallery =
registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
// Callback is invoked after the user selects a media item or closes the
// photo picker.
if (uri != null) {
Log.d("PhotoPicker", "Selected URI: " + uri);
Bitmap image = getImageResult(uri);
if(image != null) {
addImageToGallery(image);
}
addImageToGallery(image);
} else {
Log.d("PhotoPicker", "No media selected");
}
});

Expand Down Expand Up @@ -455,7 +459,10 @@ private void takePicture(){
* Open the phone's image gallery to add an image
*/
private void addFromGallery(){
getImageFromGallery.launch("image/*");
ActivityResultContracts.PickVisualMedia.VisualMediaType mediaType = (ActivityResultContracts.PickVisualMedia.VisualMediaType) ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE;
getImageFromGallery.launch(new PickVisualMediaRequest.Builder()
.setMediaType(mediaType)
.build());
}


Expand Down Expand Up @@ -568,14 +575,14 @@ public void onRequestPermissionsResult(int requestCode, @NotNull String[] permis
flagged = true;
}
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
if (ContextCompat.checkSelfPermission(FeatureViewActivity.this,
if (ContextCompat.checkSelfPermission(FeatureViewActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),
"Storage permissions required",
Toast.LENGTH_SHORT).show();
flagged = true;
flagged = true;

}
}
}
if(!flagged) {
takePicture();
Expand Down

0 comments on commit 86dcc27

Please sign in to comment.