5

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?

user19922
  • 175
  • 1
  • 3
  • 12

4 Answers4

7

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();
        }
    }
}
Hakim Douib
  • 495
  • 1
  • 6
  • 13
Vanraj Ghed
  • 1,261
  • 11
  • 23
  • Thank you man, it looks like it's OK. BUT, not working with to large images (aprox. 4MB). It doesn't throw an exception, but either do not display the image. It shows gray image instead, like the image it's too heavy to upload or sth. Anyway to complete this answer, to convert `Bitmap` to `Bytearray` this helped me: http://stackoverflow.com/questions/4989182/converting-java-bitmap-to-byte-array. – user19922 Jun 03 '16 at 15:48
  • @user1992 Always Welcome – Vanraj Ghed Jun 04 '16 at 05:00
1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I use `ByteArrayOutputStream` somewhere else (not shown in the code). So, any solution that might be suggested should take this in count. Thank you! – user19922 Jun 03 '16 at 12:57
  • @user1992: Then 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`. Use that to populate your `ByteArrayOutputStream`, and use the `byte[]` from the `ByteArrayOutputStream` and `BitmapFactory.decodeByteArray()` to create your bitmap. – CommonsWare Jun 03 '16 at 12:59
0

you can use like below code

public void pickImage() {
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  intent.setType("image/*");
  startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
        if (data == null) {
            //Display an error
            return;
        }
        InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
        //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
    }
}
swetha
  • 23
  • 6
  • I don't see any thing different here? Am I missing something? – user19922 Jun 03 '16 at 12:57
  • @user1992: You are missing the fact that this answer uses `getContentResolver().openInputStream()` to get an `InputStream` on the content backed by the `Uri`. – CommonsWare Jun 03 '16 at 13:00
-1

//you are in fragment so use the below code hope its work final Uri imageUri = data.getData();

            final InputStream imageStream =getActivity().getContentResolver().openInputStream(imageUri);

            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
Mazhar Iqbal
  • 813
  • 7
  • 7
  • Sorry but: 1. No improvement than existing answers; 2. Text and code not formated; 3. It is not `fragment` that caused this specific problem. Thanks – Meow Cat 2012 Mar 16 '21 at 09:49