Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setImageBitmap() is not working

I have and ImageView and it is supposed to be filled with an image from Gallery with this code:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
            getIntent.setType("image/*");
            Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            pickIntent.setType("image/*");

            Intent chooserIntent = Intent.createChooser(getIntent, "Pick an image.");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});

            startActivityForResult(chooserIntent, PICK_IMAGE_REQUEST);

Now, everything's fine in this part of the code, the problem is in onActivityResult(), exactly in setImageBitmap() because imageView is not adopting the image from Gallery. This is the code from onActivityResult():

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

    if(requestCode == PICK_IMAGE_REQUEST || resultCode == PromoUpload.RESULT_OK){

        Uri selectedImage = data.getData();
        String[] filepathColumm = {MediaStore.Images.Media.DATA};

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

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

        Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
        imagePromo.setImageBitmap(bitmap);
        pictureFlag = 1;

    }

I Toast the picturePath attribute and it's displayed ok, the problem is exactly here:

Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
    imagePromo.setImageBitmap(bitmap);

But I don't know what I'm doing wrong. This is the XML of the ImageView:

<ImageView
        android:id="@+id/imageView_promo"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:padding="1dp" />

I also tried selecting images from multiple sizes, but it doesn't seem to work...

like image 653
I'l Follio Avatar asked Mar 09 '23 16:03

I'l Follio


1 Answers

But I don't know what I'm doing wrong

You are assuming that every Uri is from the MediaStore and that every entry in the MediaStore has a useful path. Neither is true.

What you should be doing is passing selectedImage to an image-loading library (Glide, Picasso, etc.), as those libraries not only know how to use the Uri properly, but they will do the image-loading on a background thread, rather than freezing the UI as you are doing here. You can also teach the image-loading library to scale the image to fit your ImageView, to save on heap space.

A more direct replacement for your code would be:

    Uri selectedImage = data.getData();
    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));
    imagePromo.setImageBitmap(bitmap);
    pictureFlag = 1;

But, as noted, this will freeze your UI while the disk I/O and image decoding is going on.

like image 66
CommonsWare Avatar answered Mar 29 '23 09:03

CommonsWare