Grab image from the gallery

Asked

Viewed 45 times

0

Manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera.any" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />

Activity

public void TirarFoto(View view)
{
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent,1);
}

public void PegarImagem(View view)
{

    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(Intent.createChooser(intent, "Selecione uma imagem"), 12);
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    if(requestCode == 1) { //funciona
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Bitmap bitmap = (Bitmap) bundle.get("data");

                ImageView img = (ImageView) findViewById(R.id.imgFoto);
                img.setImageBitmap(bitmap);
            }
        }
    }

    if(requestCode == 12)
    {
        if(resultCode == RESULT_OK)
        {
            Uri uri = intent.getData();
            Cursor cursor = getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
            cursor.moveToFirst();
            int indexColuna = cursor.getColumnIndex(MediaStore.Images.Media.DATA); // aqui sempre vem 0
            String path = cursor.getString(indexColuna);
            cursor.close();
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            ImageView img = (ImageView) findViewById(R.id.imgFoto);
            img.setImageBitmap(bitmap);
        }
    }
}

The first time I did it would have worked but I don’t know what’s different about it that it doesn’t work now. Select image but not arrow in Imagemview.

I tried to set direct from Uri but gave file error not found.

1 answer

1

Good night,

Here is the Onclick button to open Gallery:

findViewById(R.id.bt_imagem).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(Intent.createChooser(intent, "Selecione uma imagem"), 12);
        }
    });

Follow the onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 12 && resultCode == Activity.RESULT_OK && data != null) {
        if (data.getData() != null) {
            String[] filePath = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(data.getData(), filePath, null, null, null);
            if (c != null) {
                c.moveToFirst();
                Bitmap bitmap = BitmapFactory.decodeFile(c.getString(c.getColumnIndex(filePath[0])));
                c.close();
                Bitmap bitmapReduzido = Bitmap.createScaledBitmap(bitmap, 1080, 1000, true);
                ImageView img = (ImageView) findViewById(R.id.imgFoto);
                img.setImageBitmap(bitmapReduzido);
            }
        }
    }
}

If you are using an Android 23+ API, you have to allow the application to read the Storage files, otherwise it will not upload the photo.

I hope I’ve helped.

Browser other questions tagged

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