Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use setType() on intent to allow only jpeg and png images; not gif images

I'm developing an Android app where a user has to select an image from the gallery. This image should have either .jpeg/.png extension.

I have tried:

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/jpeg, image/png");

However, it still allows me to select .gif images. What am I doing wrong?

like image 454
Vikram Bajaj Avatar asked Jan 13 '17 17:01

Vikram Bajaj


People also ask

How to share image from Uri in Android programmatically?

Intent sharingIntent = new Intent(Intent. ACTION_SEND); Uri imageUri = Uri. parse("http://stacktoheap.com/images/stackoverflow.png"); sharingIntent. setType("image/png"); sharingIntent.

What is Intent action_ send?

Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type. The system automatically identifies the compatible activities that can receive the data and displays them to the user.


Video Answer


1 Answers

Your code should look like this, if you want to specify the image formats that you want to allow:

String[] mimeTypes = {"image/jpeg", "image/png"};
final Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            .setType("image/*")
            .putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
like image 144
Stepan Sanda Avatar answered Nov 14 '22 23:11

Stepan Sanda