Android app to select and display images from the gallery

This article is the 22nd day article of "Inobeko Summer Vacation Advent Calendar 2020" planned by Fujitsu Systems Web Technology Limited. (Promise) The content of this article is my own opinion and does not represent the organization to which I belong.

Introduction

I wrote the source to select an image and display it on the screen in Andoroid Studio, so this article will introduce the source, procedure, etc.

background

Based on the previous article (Try to judge food photos using Google Cloud Vision API), judge selected photos with API I decided to make an app, and as the first step, I made an article about selecting images from the gallery.

Advance preparation

Regarding the following, the article on the 16th day of this Advent calendar "I made an Android application" Hello World "and tried to play with it freely ) ”, So please refer to it. I will omit it here.

・ Installation of Android Studio ・ Create a new project -Create and start an emulator

Japanese localization of emulator

Probably it is in English by default, so I will translate it into Japanese. Display the [Settings] screen on the emulator and select [System]-[Languages & input]-[Languages]. I think that only English is displayed, so if you select Japanese from [Language preferences], Japanese will be displayed under English, so you can change the order by dragging to complete Japanese localization.

エミュレーター_日本語化.PNG

Save the image on an emulator

Allows the image to be viewed in an emulator for image selection.

There are two methods -Drag and drop the image onto the emulator The image will be in the DownLoad folder. -Upload with Device File Explorer Open it with [View]-[Tool Windows]-[Device File Explorer]. You can add files by right-clicking in any folder.

Source

Press the button to open the gallery and display the selected image on the screen.

Open gallery

Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);

Intent (type of action, target app) startActivityForResult (intent, request code) You can start the activity in another app with the Intent class. This time, I open the gallery (image list) and get the selected image.

See General Intents (https://developer.android.com/guide/components/intents-common?hl=ja) for more information on Intents.

The contents specified in the argument have the following roughly meanings. -Intent.ACTION_PICK: Action that returns the selected data -Media.EXTERNAL_CONTENT_URI: URI from which image data can be acquired

The request code specified in the startActivityForResult parameter is for determining the transition source. Here, 1 is specified by hard coding, but I feel that it is better to manage it with constants.

Display selection results

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);

      if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
            ImageView imgView = findViewById(R.id.imageView);
            BufferedInputStream inputStream = null;
            try {
                inputStream = new BufferedInputStream
                             (getContentResolver().openInputStream(data.getData()));
                imgView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
      }
}

The "onActivityResult" method receives the execution result of "startActivityForResult" in the previous stage. Sets the selected result to the ImageView of the screen.

Also, as the argument requestCode, the request code specified in the "startActivityForResult" method is returned. As mentioned in the previous section, the transition source can be determined, so you can write as follows.

public void button1_onClick(View view) {
      Intent intent = new Intent(Intent.ACTION_PICK, Media.INTERNAL_CONTENT_URI);
      startActivityForResult(intent, 1);
}

public void button2_onClick(View view) {
      Intent intent = new Intent(Intent.ACTION_PICK, Media.INTERNAL_CONTENT_URI);
      startActivityForResult(intent, 2);
}
    
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);

      if (requestCode == 1) {
            //Processing for button1
      } else if (requestCode == 2) {
            //Processing for button2
      }
}

screen

When the above sources were executed on the emulator, the original purpose of "selecting an image and displaying it on the screen" was achieved.

① Press the button ② ③ Select an image ④ Display the selected image

image.png image.png image.png image.png

Finally

I used Andoroid Studio for the first time, but I didn't have much trouble because the source was similar to the Form application.

Next time, I would like to try calling the Cloud Vision API from the Andoroid app.

Recommended Posts

Android app to select and display images from the gallery
How to narrow down the image format from the gallery on Android and then select and import multiple images
[Android] Display images and characters on the ViewPager tab
[Android] Uploading images from your device to the server
Trial and error to display national holidays in Android app development. Part 2
POST images from Android to PHP using Retrofit
Android app: Try to summarize events and listeners
Clone and duplicate the app from GitHub (initialization)
How to connect to lcalhost from your smartphone and use the app under development
Vibrate the wristband device with Bluetooth from the Android app
Kotlin may take the world from App to Web
How to transition from the [Swift5] app to the iPhone settings screen
How to display the select field of time_select every 30 minutes
Hold down the practical and essential images to tackle TDD
Java source sample (Oracle Database + java) to SELECT and display CLOBs
How to create a form to select a date from the calendar
Save and display multiple images
Learn how to customize the Navigation Bar from the sample app
[Android] Change the app name and app icon for each Flavor
Send the accelerometer value from Android to PC via UDP
Technical causes and countermeasures for the points I was addicted to with the first Android app & Kotlin
[Swift] How to display when the quiz app answers correctly [Beginner]
[Android, Java] Method to find the elapsed date from two dates
I want to display the images under assets/images in the production environment
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
[Java] How to convert from String to Path type and get the path
Introduction to Android App Development 1 Installing JDK and Android Studio for mac
[Android] I want to get the listener from the button in ListView
Android Development-Try to display a dialog-
[Android] Display Snackbar from any position
The road from JavaScript to Java
Update JAVA to the latest version to 1.8.0_144 (when downloading from the web and updating)
[Android] How to turn the Notification panel on and off using StatusBarManager
From setting up Firebase's Realtime Database to simple data entry (Android app)
[Java] How to get the current date and time and specify the display format
[Android] How to pass images and receive callbacks when sharing using ShareCompat
Confirmation and refactoring of the flow from request to controller in [httpclient]
The story of releasing the Android app to the Play Store for the first time.
Trial and error to display national holidays in Android application development. Part 1
I translated the grammar of R and Java [Updated from time to time]
Is it possible to put the library (aar) in the Android library (aar) and use it?
The Android app crashes. Just click a button and it will fall.
Investigate the replacement from Docker to Podman.
Sample to display (head-up) notifications on Android
[Ruby] From the basics to the inject method
* Android * Saving / loading images to external storage
How to find the tens and ones
Try using the Emotion API from Android
Soaring tech skills and declining tech skills from 2014 to 2019
Pre-processing to display on the browser (compiler)
Java to C and C to Java in Android Studio
[heroku deployment procedure ③] From Ruby version specification to deployment and access to the application (complete)
Get YouTube video information with Retrofit and keep it in the Android app.
[Java] Get and display the date 10 days later using the Time API added from Java 8.
[Java] Program example to get the maximum and minimum values from an array
[swift5] How to transition from the app to an external site by specifying the URL
Android development-WEB access (POST) Try to communicate with the outside and send data. ~
If you are using Android Room and want to change the column definition
Try to access the on-premise system from SAP Cloud Platform --Java App Edition
I'm making an Android app and I'm stuck with errors and how to solve it
[JDBC ③] I tried to input from the main method using placeholders and arguments.