2

friends,

i am trying to display image from gallery now i want to put check if selected file is image then it should be displayed.

using following code any one help me out how to achieve this?

User can select video file, image file etc.. anything so i want to allow only images.

ON button click
  private void SelectImageFromGallery()
            {

                 startActivityForResult(new Intent(Intent.ACTION_PICK,
                 android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),0);


            }


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

                switch( resultCode )
                {
                    case 0:
                        Log.i( "MakeMachine", "User cancelled" );
                        break;
                    case -1:
                        _data = data;
                        ShowImageInActivity();
                        break;
                }
            }
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

1 Answers1

3
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, IMAGE_PICK);

No need to check if you want to pick only images then use above property. Same case with audio and video files, it automatically filters your required files.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278
  • This works and is the correct solution, but in certain devices, if there is third party app installed, like EsFileExplorer, they will let the user choose files that are not of type images. Not that it is a fault of the answer you suggested, but I wish it could be prevented. – rgv Jun 15 '16 at 21:52