Gallery Android image does not work (Android 6.0 and Android 5.1.1)

I use the code below to select an image file from the 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 method onActivityResult:

@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 helper method for extracting the 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 application is weird. In my emulator 6.0, sometimes it works, and sometimes not. On another device (Android 5.1.1), this line is addedFileNotFound Exception

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

All necessary permissions are beautiful. Do you guys know what's going on here? Or do you have any better suggestion on what to use to select images from the gallery?

+4
source share
4 answers

, 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();
        }
    }
}
+3

# 1: ByteArrayOutputStream, .

# 2: photoPath = getPath(data.getData()); getPath(), .

№ 3: , ImageView, Uri (data.getData()) . , getContentResolver().openInputStream(), InputStream , Uri.

+1

you can use as 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...
    }
}
0
source

This link can help you:

http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample

You can also switch to third-party integration, for a better result:

https://github.com/coomar2841/image-chooser-library

0
source

All Articles