Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android pick image from gallery not working (Android 6.0 & Android 5.1.1)

I'm using the code below to pick an image file from device gallery:

First I call this piece of code:

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

This is my onActivityResult method:

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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

            photoPath  = getPath(data.getData());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileInputStream fis;
            try {
                fis = new FileInputStream(new File(photoPath));
                byte[] buf = new byte[1024];
                int n;
                while (-1 != (n = fis.read(buf))) {
                    baos.write(buf, 0, n);
                }

                img.setImageBitmap(BitmapFactory.decodeFile(photoPath));

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

And this a helper method to retrieve image path:

private String getPath(Uri uri) {

        String[]  data = { MediaStore.Images.Media.DATA };
        CursorLoader loader = new CursorLoader(getApplicationContext(), uri, data, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

My problem is that the app is weird. In my 6.0 emulator, sometimes it works and sometimes No. In an other device (Android 5.1.1) an FileNotFound Exception is thrown at this line

fis = new FileInputStream(new File(photoPath));

All required permissions are fine.Do you guys have any idea what's going on here? Or do you have any better suggestion what to use to pick an image from gallery?

like image 511
user19922 Avatar asked Jun 03 '16 12:06

user19922


2 Answers

Try this you can directly set image from onActivityResult

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

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
try{
            final Uri imageUri = data.getData();
            final InputStream imageStream = getContentResolver().openInputStream(imageUri);
            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

            imageView.setImageBitmap(selectedImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
like image 196
Vanraj Ghed Avatar answered Sep 27 '22 18:09

Vanraj Ghed


Step #1: Delete the ByteArrayOutputStream, as you are not using it.

Step #2: Delete photoPath = getPath(data.getData()); and the getPath() method, as they are wrong.

Step #3: Use an image loading library to asynchronously populate your ImageView, passing it the Uri (data.getData()) of the image to load. Or, roll lots of your own image-loading code, including forking a background thread and using getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri.

like image 42
CommonsWare Avatar answered Sep 27 '22 19:09

CommonsWare