27
27
import android .widget .ImageView ;
28
28
import android .widget .TextView ;
29
29
import android .widget .Toast ;
30
+ import androidx .activity .result .ActivityResultLauncher ;
31
+ import androidx .activity .result .PickVisualMediaRequest ;
32
+ import androidx .activity .result .contract .ActivityResultContracts .PickVisualMedia ;
30
33
import androidx .annotation .NonNull ;
31
34
import androidx .annotation .Nullable ;
32
35
import androidx .appcompat .app .AppCompatActivity ;
@@ -100,19 +103,42 @@ private void setupView() {
100
103
findViewById (R .id .backButton ).setOnClickListener (v -> finish ());
101
104
findViewById (R .id .sendButton ).setOnClickListener (this ::submitFeedback );
102
105
106
+ // Registers a photo picker activity launcher in single-select mode
107
+ ActivityResultLauncher <PickVisualMediaRequest > pickMedia =
108
+ registerForActivityResult (
109
+ new PickVisualMedia (),
110
+ uri -> {
111
+ if (uri != null ) {
112
+ LogWrapper .d (TAG , "Selected custom screenshot URI: " + uri );
113
+ screenshotUri = uri ;
114
+ setupScreenshot ();
115
+ } else {
116
+ LogWrapper .d (TAG , "No custom screenshot selected. Not changing screenshot URI." );
117
+ }
118
+ });
119
+
120
+ findViewById (R .id .chooseScreenshotButton )
121
+ .setOnClickListener (
122
+ v ->
123
+ pickMedia .launch (
124
+ new PickVisualMediaRequest .Builder ()
125
+ .setMediaType (new PickVisualMedia .SingleMimeType ("image/png" ))
126
+ .build ()));
127
+
103
128
setupScreenshot ();
104
129
}
105
130
106
131
private void setupScreenshot () {
107
132
blockingExecutor .execute (
108
133
() -> {
109
- // do I/O on separate thread in order to not block the UI
110
- Bitmap screenshot = screenshotUri == null ? null : readScreenshot ();
134
+ // Do I/O on separate thread in order to not block the UI
135
+ Bitmap screenshot = readScreenshot (screenshotUri );
111
136
if (screenshot != null ) {
112
137
runOnUiThread (
113
138
() -> {
114
139
ImageView imageView = findViewById (R .id .screenshotImageView );
115
140
imageView .setImageBitmap (screenshot );
141
+ imageView .setVisibility (VISIBLE );
116
142
CheckBox checkBox = findViewById (R .id .screenshotCheckBox );
117
143
checkBox .setChecked (true );
118
144
checkBox .setOnClickListener (
@@ -132,21 +158,21 @@ private void setupScreenshot() {
132
158
}
133
159
134
160
@ Nullable
135
- private Bitmap readScreenshot () {
161
+ private Bitmap readScreenshot (@ Nullable Uri uri ) {
162
+ if (uri == null ) {
163
+ return null ;
164
+ }
136
165
Bitmap bitmap ;
137
166
try {
138
167
bitmap =
139
168
ImageUtils .readScaledImage (
140
- getContentResolver (),
141
- screenshotUri ,
142
- SCREENSHOT_TARGET_WIDTH_PX ,
143
- SCREENSHOT_TARGET_HEIGHT_PX );
169
+ getContentResolver (), uri , SCREENSHOT_TARGET_WIDTH_PX , SCREENSHOT_TARGET_HEIGHT_PX );
144
170
} catch (IOException | SecurityException e ) {
145
- LogWrapper .e (TAG , "Could not read screenshot image from URI: " + screenshotUri , e );
171
+ LogWrapper .e (TAG , "Could not read screenshot image from URI: " + uri , e );
146
172
return null ;
147
173
}
148
174
if (bitmap == null ) {
149
- LogWrapper .e (TAG , "Could not decode screenshot image: " + screenshotUri );
175
+ LogWrapper .e (TAG , "Could not decode screenshot image: " + uri );
150
176
}
151
177
return bitmap ;
152
178
}
0 commit comments