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,14 +103,36 @@ 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
() -> {
@@ -132,21 +157,21 @@ private void setupScreenshot() {
132
157
}
133
158
134
159
@ Nullable
135
- private Bitmap readScreenshot () {
160
+ private Bitmap readScreenshot (@ Nullable Uri uri ) {
161
+ if (uri == null ) {
162
+ return null ;
163
+ }
136
164
Bitmap bitmap ;
137
165
try {
138
166
bitmap =
139
167
ImageUtils .readScaledImage (
140
- getContentResolver (),
141
- screenshotUri ,
142
- SCREENSHOT_TARGET_WIDTH_PX ,
143
- SCREENSHOT_TARGET_HEIGHT_PX );
168
+ getContentResolver (), uri , SCREENSHOT_TARGET_WIDTH_PX , SCREENSHOT_TARGET_HEIGHT_PX );
144
169
} catch (IOException | SecurityException e ) {
145
- LogWrapper .e (TAG , "Could not read screenshot image from URI: " + screenshotUri , e );
170
+ LogWrapper .e (TAG , "Could not read screenshot image from URI: " + uri , e );
146
171
return null ;
147
172
}
148
173
if (bitmap == null ) {
149
- LogWrapper .e (TAG , "Could not decode screenshot image: " + screenshotUri );
174
+ LogWrapper .e (TAG , "Could not decode screenshot image: " + uri );
150
175
}
151
176
return bitmap ;
152
177
}
0 commit comments