ahmed adel
ahmed adel

Reputation: 89

how to get image from gallery

I'm trying to get image from the gallery of my device ... I'm having a dialog that has a button that initializes the gallery after the user chooses a photo the path of it should be returned ...

but the path is returned as null

that's how I do it -

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

In my onActivityResult -

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                photo_path = getPath(selectedImageUri);
                // photo_path=selectedImageUri.getPath();
                Globals.galleryFlag = true;
                DialogFragment pindiDialogFragment = new PinPhotoDialogFragment();
                pindiDialogFragment.show(getFragmentManager(), "pin photo");
                dismiss();
            }
        }
    }

And that's my getPath function -

    public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity.getContentResolver().query(uri,
            projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Upvotes: 1

Views: 4499

Answers (2)

Psypher
Psypher

Reputation: 10829

Call intent as below:

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

Correct OnActivityResult as below:

 @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) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

Source: Android get image from gallery into ImageView

Upvotes: 0

karvoynistas
karvoynistas

Reputation: 1285

Few months ago I faced a similar situation. You cannot assume that the Uri returned by the media picker will correspond to a local file , so the best approach is to use a ContentResolver to treat the picture as a stream.

First of all open the gallery activity with some request code (0 for this example)

 Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
 startActivityForResult(i, 0);

Then override the onActivityResult method to get the bitmap from gallery.

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

    if (requestCode == 0 && resultCode == RESULT_OK && null != data) { // we have bitmap from filesystem!
        Uri selectedImage = data.getData();

        InputStream inputStream = null;

        if (ContentResolver.SCHEME_CONTENT.equals(selectedImage.getScheme())) {
            try {
                inputStream = this.getContentResolver().openInputStream(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            if (ContentResolver.SCHEME_FILE.equals(selectedImage.getScheme())) {
                try {
                    inputStream = new FileInputStream(selectedImage.getPath());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            rpView.setReportPicture(bitmap,false); 
}

Upvotes: 2

Related Questions