MikkoP
MikkoP

Reputation: 5092

Getting image extension

In the application the user can select an image. To start the gallery I use the following code:

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

When I print the path in onActivityResult method, I get the image path like /external/images/media/5615. I'd like to know the image extension, because the image needs to be uploaded to a server. The extension may vary.

Upvotes: 3

Views: 959

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82553

What you're getting is a path the MediaStore uses to get the Images. The following code will take the result from your Intent, and get the filename attached to it:

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

        if (requestCode == ACTIVITY_SELECT_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            Log.i(TAG, "" + selectedImage.toString());
            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();

            Log.i(TAG, "" + picturePath);

            //Do whatever with picturePath

        }

Upvotes: 2

Related Questions