K
K
kirawa2015-10-01 13:24:09
Android
kirawa, 2015-10-01 13:24:09

How to get an image from the gallery in onActivityResult?

I have a Navigation Drawer which has a fragment with a file upload to the server. This is a class that inherits from Fragment.
I want to get the path to pictures and videos.

Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
        galleryIntent.setType("image/* video/*");
        getActivity().startActivityForResult(galleryIntent, RESULT_LOAD);


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == RESULT_LOAD && resultCode == Activity.RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                Cursor cursor = getActivity().getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                FULL_PATH_TO_LOCAL_FILE = cursor.getString(columnIndex);
                cursor.close();
            } else {
                //Toast.makeText(PlaceholderFragmentOnDutyCity.this, "You haven't picked Image", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            //Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
        }
    }

FULL_PATH_TO_LOCAL_FILE = null
Please help me understand.
If the class is inherited from Activity, then everything works.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question