0

I am trying to load an image from an android device into my application.

I've already used the permission in execution time in my code because I found it was necessary in the recents Api but it doesn't work the same way.

I can click the button and choose the image, but it doesn't appears in my ImageView.

Here is my ActivityCode:

package geoapp.testemapas.com.geoapp;

import android.app.VoiceInteractor;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;

public class PegarImagemActivity extends AppCompatActivity {

public static final int IMAGEM_ID = 1;
public static final int PERMISSAO_REQUEST = 2;

private Button pegarImgemBotaoPegarImagem;
private Button PegarImgemBotaoEnviarImagem;
private ImageView pegarImagemImageViewImgem;

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

    pegarImgemBotaoPegarImagem = (Button) findViewById(R.id.pegar_imagem_botao_pegar_imagem);
    pegarImagemImageViewImgem = (ImageView) findViewById(R.id.pegar_imagem_iv);

    pegarImgemBotaoPegarImagem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, IMAGEM_ID);
        }
    });

    if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
        if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)){

        }else{
            ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSAO_REQUEST );
        }
    }
}

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

            Uri imagem_selecionada = intent.getData();
            String[] colunas =  {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(imagem_selecionada, colunas, null, null, null);
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(colunas[0]);
            String pathImg = cursor.getString(index);
            cursor.close();
            Bitmap bitmap = BitmapFactory.decodeFile(pathImg);
            pegarImagemImageViewImgem.setImageBitmap(bitmap);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSAO_REQUEST){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

        }else{

        }
        return;
    }
}
}
ChrisM
  • 1,576
  • 6
  • 18
  • 29

2 Answers2

0

You can try this method.

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
1001);

on activity result, you will get the image selected and you can show it in your image view

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == 1001) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
            filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        bitmap = BitmapFactory.decodeFile(picturePath);

        if (bitmap != null) {
            ImageView img = (ImageView)findViewById(R.id.imgeview_id);
            img.setImageBitmap(bitmap);
        }
    }
 }
0

Looks good! few corrections are required to make it working for you.

  1. add uses permission on Manifest file.

  2. correct code mistake.

    if (resultCode == RESULT_OK){

    }

That's it :)

Pankaj Kant Patel
  • 2,050
  • 21
  • 27