How to add a picture from the gallery in Android Studio

Images are one of the important objects in Android applications to make the Android applications we create look more attractive and not just show text objects in the application. To use the function of adding pictures from gallery in the Android application, you can follow the tutorial below with the Android Studio software.

Also Read: How To Add Pictures From Camera In Android Studio


Reference material: Android Studio manual

1. Add one button and Image view At Main activity. When the button is pressed and the user selects an image, the image is displayed on the screen Image view

button and Image view

Write the following code:


2. Instantiation button and Image view that you created in the previous step. Customize the ID if the ID is enabled layout

Write the following code:

package com.example.abifirmandhani.inwepo;import android.content.Intent;import;import;import android.provider.MediaStore;import;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import;import;public class MainActivity extends AppCompatActivity {    //instansiasi database firebase    FirebaseDatabase database ;    DatabaseReference myRef;    EditText text1;    EditText text2;    EditText text3;    EditText text4;    ImageView img1;    Button btn1;    final int kodeGallery = 100, kodeKamera = 99;    Uri imageUri;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        database = FirebaseDatabase.getInstance();        text1 = (EditText) findViewById(;        text2 = (EditText) findViewById(;        text3 = (EditText) findViewById(;        text4 = (EditText) findViewById(;        img1 = (ImageView) findViewById(;        btn1 = (Button) findViewById(;         btn1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intentGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                startActivityForResult(intentGallery, kodeGallery);            }        });    }        }

function setOnclickListener is when button then the command in onClick method is carried out. The command here is to open the gallery.

onActivityResult method

3. Create onActivityResult method, The function of method it is contageous Result of every conjuration intention. Because in the previous step you ran intention with startActivityForResult.

Write the following code:

@Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if(requestCode == kodeGallery && resultCode == RESULT_OK){            imageUri = data.getData();            img1.setImageURI(imageUri);        }

A condition selection is made in the code, this is done because it is not just the results from the gallery that flow into the code onActivityResult method. Therefore, a selection of conditions is made whether inquiry The code is codeGallery. Because while running intention to adjust inquiry The code is codeGallery.

If it meets the requirements, the data will be in the form of. changed URI. then Image view will be assessed URI the.

4. Add permission At Android manifestso that your app can access the gallery.

Write the following code:

5. When the download is carried out and the upload button is pressed, the gallery opens and you can select one of the images.

Application example

Here are the results that the image was successfully added to:

Complete. In the next tutorial we are going to discuss how to add pictures, but with your phone camera.

Leave a Reply

Your email address will not be published.