5

In my application I have an option which allows a user to pick an image from gallery and use it in the application. However I would want to restrict the user to select only images with .jpg extension. How do I achieve that?

So far I've tried the following, but each type of image is at the disposal with this approach:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, RESULT_LOAD_IMAGE);
Skynet
  • 7,820
  • 5
  • 44
  • 80

1 Answers1

8

1. You can use

intent.setType("image/jpeg");

or

intent.setType("image/jpg");

2. Or you can use the onActivityResult to check the file using :

filePath.endsWith(".jpg")

if the files are of kind .jpg or .jpeg then only implement your logic else Toast that the file should be in .jpg format.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
Shail Adi
  • 1,502
  • 4
  • 14
  • 35
  • 1
    I tried both as stated above by @dtmilano but its not working, even videos on the gallery are shown. I will give filePath.endsWith(".jpg); option a try now. – Skynet Feb 22 '13 at 06:52
  • how to set multiple types here. Like jpeg, png, mp4 and other ? – xaif Jan 05 '21 at 15:39