1111import android .graphics .BitmapFactory ;
1212import android .net .Uri ;
1313import android .os .Bundle ;
14+ import android .util .Log ;
1415import android .view .View ;
1516import android .view .ViewGroup ;
1617import android .widget .Button ;
1718import android .widget .EditText ;
1819import android .widget .ProgressBar ;
19- import android .widget .TextView ;
2020
2121import androidx .activity .result .ActivityResult ;
2222import androidx .activity .result .ActivityResultCallback ;
2323import androidx .activity .result .ActivityResultLauncher ;
24+ import androidx .activity .result .PickVisualMediaRequest ;
2425import androidx .activity .result .contract .ActivityResultContracts ;
2526import androidx .annotation .NonNull ;
2627import androidx .appcompat .app .AppCompatActivity ;
2728
29+ import java .io .FileNotFoundException ;
2830import java .io .IOException ;
29-
30-
31-
31+ import java .io .InputStream ;
3232
3333
3434class Listener implements View .OnClickListener {
@@ -56,6 +56,7 @@ class IoHandler {
5656
5757 Button ButtonOkay ;
5858 Button ButtonCancel ;
59+ Button ButtonCooseImgGalery ;
5960
6061 ProgressBar ProgressBar ;
6162 MainActivity Main ;
@@ -73,13 +74,15 @@ public IoHandler(SharedPreferences sharedPreferences, EditText nameEdit, EditTex
7374 ButtonConnect = buttonConnect ;
7475 ButtonCancel = main .dialog .findViewById (R .id .btn_cancel );
7576 ButtonOkay = main .dialog .findViewById (R .id .btn_okay );
77+ ButtonCooseImgGalery = main .findViewById (R .id .btn_chooseGalery );
7678
7779 ProgressBar =progessBar ;
7880 Main = main ;
7981
8082 ButtonConnect .setOnClickListener (new Listener (this ));
8183 ButtonCancel .setOnClickListener (new Listener (this ));
8284 ButtonOkay .setOnClickListener (new Listener (this ));
85+ ButtonCooseImgGalery .setOnClickListener (new Listener (this ));
8386
8487
8588
@@ -106,6 +109,8 @@ public void Save() {
106109 Editor .commit ();
107110 }
108111
112+
113+
109114 public void OnClick (View view ) {
110115 if (ButtonConnect .equals (view )) {
111116 Main .GetImage ();
@@ -116,6 +121,8 @@ else if (ButtonCancel.equals(view)){
116121 else if (ButtonOkay .equals (view )){
117122 Main .download ();
118123 Main .dialog .dismiss ();
124+ } else if (ButtonCooseImgGalery .equals (view )) {
125+ Main .ChooseImgGallery ();
119126 }
120127 }
121128
@@ -171,6 +178,12 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
171178
172179 }
173180
181+ public void ChooseImgGallery (){
182+ pickMultipleMedia .launch (new PickVisualMediaRequest .Builder ()
183+ .setMediaType (ActivityResultContracts .PickVisualMedia .ImageOnly .INSTANCE )
184+ .build ());
185+
186+ }
174187
175188 public void GetImage (){
176189 Intent intent = new Intent (this , Camera .class );
@@ -179,7 +192,31 @@ public void GetImage(){
179192
180193
181194 }
182-
195+ private final ActivityResultLauncher <PickVisualMediaRequest > pickMultipleMedia = registerForActivityResult (
196+ new ActivityResultContracts .PickMultipleVisualMedia (), uris -> {
197+ // Callback is invoked after the user selects media items or closes the
198+ // photo picker.
199+ if (!uris .isEmpty ()) {
200+ for ( Uri uri : uris
201+ ) {
202+ BitmapFactory .Options options = new BitmapFactory .Options ();
203+ InputStream imageStream = null ;
204+ try {
205+ imageStream = getContentResolver ().openInputStream (uri );
206+ } catch (FileNotFoundException e ) {
207+ continue ;
208+ }
209+ Bitmap bitmap = BitmapFactory .decodeStream (imageStream , null , options );
210+ connection .onPicture (bitmap );
211+ try {
212+ imageStream .close ();
213+ } catch (IOException e ) {
214+ continue ;
215+ }
216+ }
217+ } else {
218+ }
219+ });
183220 private final ActivityResultLauncher <Intent > canmeraActivity = registerForActivityResult (
184221 new ActivityResultContracts .StartActivityForResult (),
185222 new ActivityResultCallback <ActivityResult >() {
0 commit comments