My image does not appear in Imageview

Asked

Viewed 1,725 times

9

I’m looking for the image in Sdcard. It happens all right, open the gallery, select 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>

  • Could you put your layout in question? My suggestion is that your ImageView is not set in size px|dp|dpi|etc.... If you cannot set, use the attribute adjustViewBounds to the ImageView adjust the size according to the size of Bitmap.

  • Posted below. @Wakim

  • @Isabella Julia but managed to get the image?

2 answers

1

In my case I see the image orientation, is very quiet, try it this way and let me know if it works, 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();
                }
            }
  • failed, rotatedBitmap does not work

1

It’s been a while since you posted this question, but if you still have this problem, I will post the solution below. I had the same problem as you and I managed to solve it in the following way.

In your method responsible for taking the image btnFoto, change the instruction Intent.ACTION_GET_CONTENT for Intent.ACTION_PICK thus, your code will be as follows:

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

This way you will be able to catch the image and it will be shown in ImageView normally.

  • But it might explain why the difference?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.