Load an Image from the Android Gallery

3

I click on the image and it lets me select one from the gallery, but when I select it, it does not change. I know I'm missing part of the code, could someone help me with it? Thanks!

MainActivity:

public class MainActivity extends AppCompatActivity {


    private static final int PHOTO_SELECTED = 1;
    ImageButton fotoH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            fotoH = (ImageButton) findViewById(R.id.pruebah);

            fotoH.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    startActivityForResult(intent, PHOTO_SELECTED);
                }
            });
        }
    }
    
asked by UserNameYo 30.12.2016 в 17:54
source

2 answers

3

Finally I have solved it. I leave here the code

private static final int PICK_IMAGE = 100;
Uri imageUri;
ImageView foto_gallery;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    foto_gallery = (ImageView)findViewById(R.id.foto_gallery);

    foto_gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openGallery();
        }
    });
}

private void openGallery(){
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode == RESULT_OK && requestCode == PICK_IMAGE){
        imageUri = data.getData();
        foto_gallery.setImageURI(imageUri);
    }
    }
    }
    
answered by 30.12.2016 / 23:05
source
3

You are accessing through the method defined in the view to perform the intent,

public void onClick(View v) {

As an option you can define in each view the index of the element by means of the Tag,

   <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="0"
            android:src="@drawable/image" />

In this way you can get the index of the selected photo:

        fotoH = (ImageButton) findViewById(R.id.pruebah);

        //Obtiene el indice de la foto selecionada a travez de la etiqueta definida (tag).
        PHOTO_SELECTED = Integer.Parse(fotoH.getTag());

        fotoH.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, PHOTO_SELECTED);
            }
        });

Although it is important to comment that what you do must be done through a Custom Intent Chooser, this to open the photo in an application that can manipulate your type of content, would be like this:

 startActivityForResult(Intent.createChooser(intent, "Selecciona Imagen"), PHOTO_SELECTED);
    
answered by 30.12.2016 в 18:25