Skip to content

Commit 74d6509

Browse files
committed
Version 0.9.2 add gallery image picker
1 parent 45a9ffb commit 74d6509

4 files changed

Lines changed: 71 additions & 20 deletions

File tree

app/build.gradle

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ plugins {
33
}
44

55
android {
6-
compileSdk 33
6+
compileSdk 34
77

88
defaultConfig {
99
applicationId "com.example.picturetopc"
1010
minSdk 28
11-
targetSdk 33
12-
versionCode 4
13-
versionName "v0.9.1"
11+
targetSdk 34
12+
versionCode 5
13+
versionName "v0.9.2"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
}
@@ -32,11 +32,11 @@ android {
3232
}
3333

3434
dependencies {
35-
implementation 'androidx.navigation:navigation-fragment:2.3.5'
36-
implementation 'androidx.navigation:navigation-ui:2.3.5'
35+
implementation 'androidx.navigation:navigation-fragment:2.7.5'
36+
implementation 'androidx.navigation:navigation-ui:2.7.5'
3737
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
38-
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
39-
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
38+
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2'
39+
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2'
4040
def camerax_version = "1.1.0"
4141
implementation "androidx.camera:camera-core:${camerax_version}"
4242
implementation "androidx.camera:camera-camera2:${camerax_version}"
@@ -46,18 +46,18 @@ dependencies {
4646
implementation "androidx.camera:camera-view:${camerax_version}"
4747
implementation "androidx.camera:camera-extensions:${camerax_version}"
4848

49-
implementation 'androidx.activity:activity:1.5.1'
50-
implementation 'androidx.fragment:fragment:1.5.1'
49+
implementation 'androidx.activity:activity:1.8.1'
50+
implementation 'androidx.fragment:fragment:1.6.2'
5151

52-
implementation 'androidx.appcompat:appcompat:1.5.1'
53-
implementation 'com.google.android.material:material:1.6.1'
52+
implementation 'androidx.appcompat:appcompat:1.6.1'
53+
implementation 'com.google.android.material:material:1.10.0'
5454
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
5555
implementation 'androidx.gridlayout:gridlayout:1.0.0'
5656

5757
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
5858
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
5959

6060
testImplementation 'junit:junit:4.13.2'
61-
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
62-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
61+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
62+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
6363
}

app/src/main/java/com/example/picturetopc/MainActivity.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@
1111
import android.graphics.BitmapFactory;
1212
import android.net.Uri;
1313
import android.os.Bundle;
14+
import android.util.Log;
1415
import android.view.View;
1516
import android.view.ViewGroup;
1617
import android.widget.Button;
1718
import android.widget.EditText;
1819
import android.widget.ProgressBar;
19-
import android.widget.TextView;
2020

2121
import androidx.activity.result.ActivityResult;
2222
import androidx.activity.result.ActivityResultCallback;
2323
import androidx.activity.result.ActivityResultLauncher;
24+
import androidx.activity.result.PickVisualMediaRequest;
2425
import androidx.activity.result.contract.ActivityResultContracts;
2526
import androidx.annotation.NonNull;
2627
import androidx.appcompat.app.AppCompatActivity;
2728

29+
import java.io.FileNotFoundException;
2830
import java.io.IOException;
29-
30-
31-
31+
import java.io.InputStream;
3232

3333

3434
class 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>() {

app/src/main/res/layout/activity_main.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@
4141
app:layout_constraintTop_toTopOf="parent"
4242
app:layout_constraintVertical_bias="0.186" />
4343

44+
<Button
45+
android:id="@+id/btn_chooseGalery"
46+
android:layout_width="114dp"
47+
android:layout_height="56dp"
48+
android:layout_marginStart="16dp"
49+
android:layout_marginTop="4dp"
50+
android:text="Choose gallery"
51+
app:layout_constraintBottom_toTopOf="@+id/IpAdress"
52+
app:layout_constraintEnd_toEndOf="parent"
53+
app:layout_constraintHorizontal_bias="0.46"
54+
app:layout_constraintStart_toStartOf="@+id/guideline2"
55+
app:layout_constraintTop_toTopOf="parent"
56+
app:layout_constraintVertical_bias="0.262" />
4457
<Button
4558
android:id="@+id/ConnectBtn"
4659
android:layout_width="204dp"
@@ -90,4 +103,5 @@
90103
app:layout_constraintGuide_percent="0.5"
91104
app:layout_constraintStart_toStartOf="parent" />
92105

106+
93107
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<resources>
22
<string name="app_name">PictureToPC</string>
33
<string name="IPField">Code</string>
4-
<string name="connect">Connect</string>
4+
<string name="connect">Take Picture</string>
55
<string name="done_btn">Done</string>
66
<string name="takepicture_btn">TakePicture</string>
77
<string name="NameField">Name</string>

0 commit comments

Comments
 (0)