Show Image in ImageView

0

I have a project that uses Fragments, I have a button which directs me to the image gallery, there I have to select an image and it shows in an image view, however I can not.

btnGaleria.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent,0);

        }



    });


  @Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (requestCode == IMAGEM_INTERNA) {
        if (requestCode == IMAGEM_INTERNA && resultCode == Activity.RESULT_OK) {

            Uri imagemSelecionada = intent.getData();

            String[] colunas = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContext().getContentResolver().query(imagemSelecionada, colunas, null, null, null);
            cursor.moveToFirst();

            int indexColuna = cursor.getColumnIndex(colunas[0]);
            String pathimg = cursor.getString(indexColuna);
            cursor.close();

            Bitmap bitmap = BitmapFactory.decodeFile(pathimg);
            this.image_selecionada.setImageBitmap(bitmap);
        }
        else{
            Toast.makeText(getActivity(), "Imagem não existe!", Toast.LENGTH_SHORT).show();
        }

    }
}
    
asked by anonymous 12.06.2016 / 01:20

1 answer

0

To access the mobile gallery, an android Intent is used:

Intent pickPhoto = new Intent(Intent.ACTION_PICK,
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, CONSTANTE_ESCOLHIDO);

In this way, you will receive the chosen image in the form of Uri in onActivityResult() :

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data != null && resultCode == Activity.RESULT_OK && requestCode == CONSTANTE_ESCOLHIDO) {  
        Uri photoData = data.getData();      
    }
}

So you can turn this Uri into a Bitmap and put it in an ImageView:

photoBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), photoData);
photoView.setImageBitmap(photoBitmap);
    
13.06.2016 / 02:20