Question

I am trying to get the images from sd card using Intent. Below is my code:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);

When I am running my app on samsung it's shows images to select but when I run the same code on Micromax it's showing images as well as videos.

I want to select only images. How can I solve this problem?

Était-ce utile?

La solution

I solved this problem by checking the extension of the file I am receiving and if the received file's extension is a video file's extension then I am showing user a Toast to select only photo.

Below is the method I used to get the file's extension

public static String getImageExtensionFromPath(String imagePath) {
    String[] imageNameArray = imagePath.split("/");
    String imageName = imageNameArray[imageNameArray.length - 1];
    String[] imageArray = imageName.split("\\.");
    String finalImageName = imageArray[1];
    return finalImageName;
}

Autres conseils

Add this in your AndroidManifest.xml

<intent-filter>
   <action android:name="android.intent.action.SEND" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="image/*" />
</intent-filter>

Add intent-filter into your specific listed activity. check this Google Code

There seems to be no guaranteed way to return only images by using images/*. It's up to the other apps whether or not they honor what is returned. So you can just let the user select whatever they will and then inform them if it was not an image, etc.

I use the following method after getting the URI back from the intent to detect if it contains a bona fide image, as it works even if the filename contains multiple dots (this.is.my.file.txt):

public static String getExtensionFromPath(String imagePath) { return imagePath.substring((imagePath.lastIndexOf(".") + 1), imagePath.length()); }

Remove

intent.setAction(Intent.ACTION_GET_CONTENT);

and initialize intent object by below code.

intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top