This is already part III of the tutorial for the UAT Tester App. In case you missed the previous tutorials click here:
Tutorial Part I: Setting the layout
Tutorial Part II: Setting the references in Java
In this part we will create the image picker and load the screenschot (image) to an ImageView in the app.
We want the user to be able to click on the screenshot button; and select an image from the Gallery. This image we will pass on later to an email-client installed on the device.
Add the following code to the main java class you already have (likely MainActivity.java). See some explanation below this code block.
btnscreenshot.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK){ targetUri = data.getData(); Bitmap bitmap; try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri)); thumbnailscr.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }} }
We implemented an OnClickListener for when the user clicks on the screenshot button (btnscreenshot). For this OnClick event we created an Intent which opens the native gallery application of the device. Then we used the ACTION_PICK to select an image
We use the onActivityResult method to pass the selected image to the UAT Tester App or more precise the ImageView: thumbnailscr. Before we can do this we need to to decode the file into a BitMap. In addition we use a Try /Catch block to for a FileNotFoundException (e.g. when the user returns without selecting an Image).
This is all for the 3rd Tutorial of the UAT Tester app, in this part we have successfully selected an image from our native gallery and passed it into an imageview in the app.
Click here for Part IV of the UAT Tester App.
You can also download the app: