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.
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.
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.
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
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.
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.
Press the button to open the gallery and display the selected image on the screen.
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.
@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
}
}
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
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