My image does not appear in ImageView

8

I'm looking for the image on SdCard. Everything works fine, opens the gallery, selects the image, but the selected image does not appear in ImageView.

Code:

public void btnFoto(View view){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivityForResult(intent, IMAGEM_INTERNA);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    if(requestCode == IMAGEM_INTERNA){

        if(resultCode == RESULT_OK){
            Uri imagemSelecionada = intent.getData();

            String[] colunas = {MediaStore.Images.Media.DATA};

            Cursor cursor = 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);
            ImageView iv = (ImageView) findViewById(R.id.imgEvento);
            iv.setImageBitmap(bitmap);
        }
    }

Layout:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cadastro de Evento"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal"
    style="@style/estilo"
    android:layout_marginBottom="25dp"
    android:textStyle="bold" />

<TextView
    style="@style/estilo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Título"
    android:id="@+id/txtTitulo" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edtTitulo"
    style="@style/comprimento"
    android:background="@drawable/bordas_edit"/>

<TextView
    style="@style/estilo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Descrição"
    android:id="@+id/txtDescricao" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edtDescricao"
    style="@style/comprimento"
    android:background="@drawable/bordas_edit"/>

<TextView
    style="@style/estilo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data"
    android:id="@+id/txtData" />

<EditText
    android:layout_width="190dp"
    android:layout_height="wrap_content"
    android:inputType="date"
    android:ems="10"
    android:id="@+id/edtData"
    style="@style/comprimento"
    android:background="@drawable/bordas_edit"/>

<TextView
    style="@style/estilo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Horário"
    android:id="@+id/txtHorario" />

<EditText
    android:layout_width="190dp"
    android:layout_height="wrap_content"
    android:inputType="time"
    android:ems="10"
    android:id="@+id/edtHorario"
    style="@style/comprimento"
    android:background="@drawable/bordas_edit"/>

<TextView
    style="@style/estilo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Local"
    android:id="@+id/txtLocal" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edtLocal"
    style="@style/comprimento"
    android:background="@drawable/bordas_edit"/>

<TextView
    style="@style/estilo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pontos de venda"
    android:id="@+id/textView3" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edtPontoVenda"
    style="@style/comprimento"
    android:background="@drawable/bordas_edit"
    android:layout_marginBottom="15dp"/>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right">

    <Button
    android:id="@+id/btnFoto"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_marginLeft="20dp"
    android:text="Foto"
    android:textColor="#FFF"
    android:textSize="10dp"
    android:drawableLeft="@drawable/ic_save"
    android:background="@drawable/botao"
    android:layout_gravity="right"
    android:onClick="btnFoto"/>

    <Button
        android:id="@+id/btnSalvar"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_marginLeft="20dp"
        android:text="  Salvar"
        android:textColor="#FFF"
        android:textSize="10dp"
        android:drawableLeft="@drawable/ic_save"
        android:background="@drawable/botao" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/imgEvento" />
</TableRow>

    
asked by anonymous 08.09.2015 / 16:20

2 answers

0

In my case I see the orientation of the image,    it's very quiet,    try this and let me know if it worked,    if you have a thumb, show the thumb, because it is much lighter

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

Uri selectedImageUri;
selectedImageUri = data == null ? null : data.getData(); //aqui pode estar seu erro

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 5;
                try {
                    InputStream input = getContentResolver().openInputStream(selectedImageUri);
                    final Bitmap bitmap = BitmapFactory.decodeStream(input);

                    Matrix matrix = new Matrix();
                    Cursor cursor = getApplicationContext()
                            .getContentResolver()
                            .query(selectedImageUri,
                            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

                    int orientation = 0;
                    if (cursor.getCount() == 1) {
                        cursor.moveToFirst();
                        orientation =  cursor.getInt(0);
                        matrix.preRotate(orientation);
                    }

                    if(orientation == 90){
                        rotatedBitmap = bitmapRotate(bitmap, 90);
                    }
                    else if(orientation == 180){
                        rotatedBitmap = bitmapRotate(bitmap, 180);
                    }
                    else if(orientation == 270){
                        rotatedBitmap = bitmapRotate(bitmap, 270);

                    }

                    Bitmap thumbImage = ThumbnailUtils.extractThumbnail(rotatedBitmap,
                            Constants.SignupConstants.THUMBSIZE, Constants.SignupConstants.THUMBSIZE);

                    if(thumbImage == null){
                        userImageProfile.setImageBitmap(bitmap); //setando a imagem no image view
                    }else{
                        userImageProfile.setImageBitmap(thumbImage); //setando a imagem no image view

                    }

                    convertBitmapToFile(rotatedBitmap);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
09.09.2015 / 15:28
0

It's been a while since you posted this question, but if you still have this problem, I'll post the solution below. I had the same problem as you and I managed to solve it as follows.

In your method responsible for taking the image btnFoto , change the Intent.ACTION_GET_CONTENT statement to Intent.ACTION_PICK in this way, your code will look like this:

public void btnFoto(View view){
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, IMAGEM_INTERNA);
}

In this way you will be able to get the image and it will be displayed in the ImageView normally.

    
01.09.2017 / 22:28