Skip to content

Commit c82ac50

Browse files
committed
small change to test
1 parent 13210f4 commit c82ac50

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

FaceDetection/app/src/main/java/com/tss/facedetection/MainActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import android.view.View;
1111
import android.widget.Button;
1212
import android.widget.ImageView;
13-
import android.widget.TextView;
1413

1514
import com.chaquo.python.PyObject;
1615
import com.chaquo.python.Python;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.tss.facedetection;
2+
3+
import androidx.activity.result.ActivityResultLauncher;
4+
import androidx.activity.result.contract.ActivityResultContracts;
5+
import androidx.appcompat.app.AppCompatActivity;
6+
7+
import android.app.Activity;
8+
import android.content.Intent;
9+
import android.graphics.Bitmap;
10+
import android.graphics.drawable.BitmapDrawable;
11+
import android.net.Uri;
12+
import android.os.Bundle;
13+
import android.provider.MediaStore;
14+
import android.view.View;
15+
import android.widget.Button;
16+
import android.widget.ImageView;
17+
18+
import java.io.IOException;
19+
20+
public class MainActivityForUploadImageSelection extends AppCompatActivity {
21+
22+
// One Button
23+
Button BSelectImage;
24+
25+
// One Preview Image
26+
ImageView IVPreviewImage;
27+
28+
// constant to compare
29+
// the activity result code
30+
int SELECT_PICTURE = 200;
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
setContentView(R.layout.up);
36+
37+
// register the UI widgets with their appropriate IDs
38+
BSelectImage = findViewById(R.id.BSelectImage);
39+
IVPreviewImage = findViewById(R.id.IVPreviewImage);
40+
41+
// handle the Choose Image button to trigger
42+
// the image chooser function
43+
BSelectImage.setOnClickListener(new View.OnClickListener() {
44+
@Override
45+
public void onClick(View v) {
46+
imageChooser();
47+
}
48+
});
49+
}
50+
51+
// this function is triggered when
52+
// the Select Image Button is clicked
53+
private void imageChooser()
54+
{
55+
Intent i = new Intent();
56+
i.setType("image/*");
57+
i.setAction(Intent.ACTION_GET_CONTENT);
58+
59+
launchSomeActivity.launch(i);
60+
}
61+
62+
ActivityResultLauncher<Intent> launchSomeActivity
63+
= registerForActivityResult(
64+
new ActivityResultContracts
65+
.StartActivityForResult(),
66+
result -> {
67+
if (result.getResultCode()
68+
== Activity.RESULT_OK) {
69+
Intent data = result.getData();
70+
// do your operation from here....
71+
if (data != null
72+
&& data.getData() != null) {
73+
Uri selectedImageUri = data.getData();
74+
BitmapDrawable drawable;
75+
Bitmap selectedImageBitmap;
76+
drawable = (BitmapDrawable)IVPreviewImage.getDrawable();
77+
selectedImageBitmap = drawable.getBitmap();
78+
try {
79+
selectedImageBitmap
80+
= MediaStore.Images.Media.getBitmap(
81+
this.getContentResolver(),
82+
selectedImageUri);
83+
}
84+
catch (IOException e) {
85+
e.printStackTrace();
86+
}
87+
IVPreviewImage.setImageBitmap(
88+
selectedImageBitmap);
89+
}
90+
}
91+
});
92+
93+
// this function is triggered when user
94+
// selects the image from the imageChooser
95+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
96+
super.onActivityResult(requestCode, resultCode, data);
97+
98+
if (resultCode == RESULT_OK) {
99+
100+
// compare the resultCode with the
101+
// SELECT_PICTURE constant
102+
if (requestCode == SELECT_PICTURE) {
103+
// Get the url of the image from data
104+
Uri selectedImageUri = data.getData();
105+
if (null != selectedImageUri) {
106+
// update the preview image in the layout
107+
IVPreviewImage.setImageURI(selectedImageUri);
108+
}
109+
}
110+
}
111+
}
112+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".MainActivity"
8+
tools:ignore="HardcodedText">
9+
10+
<!--Button to open the image selector-->
11+
<Button
12+
android:id="@+id/BSelectImage"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:layout_centerHorizontal="true"
16+
android:layout_marginTop="32dp"
17+
android:text="SELECT IMAGE"
18+
android:textColor="@android:color/white"
19+
android:textSize="18sp" />
20+
21+
<!--ImageView to preview the selected image-->
22+
<ImageView
23+
android:id="@+id/IVPreviewImage"
24+
android:layout_width="match_parent"
25+
android:layout_height="300dp"
26+
android:layout_below="@id/BSelectImage"
27+
android:layout_marginStart="16dp"
28+
android:layout_marginTop="16dp"
29+
android:layout_marginEnd="16dp" />
30+
31+
</RelativeLayout>

0 commit comments

Comments
 (0)