|
| 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 | +} |
0 commit comments