6

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?

Nitesh Kumar
  • 5,370
  • 5
  • 37
  • 54
  • I doubt any of the answers here are gonna make a difference. The problem most likely is that the Gallery app or whatever app is opened by the `Intent` on the Micromax device doesn't properly implement this. It should only show images, but the manufacturer messed up and now it's showing videos too. It happens more often than one might think... – Xaver Kapeller Apr 21 '14 at 10:22
  • Actually its not the gallery app which is showing videos. When I choose some other app (like photo) from the dialog then i am getting images as well as videos. – Nitesh Kumar Apr 21 '14 at 10:56
  • Well there is not much you can do about it. The code you use is fine. The apps which receive the `Intent` just don't respond like they should. I wouldn't worry about. Just implement a check if it was really a photo that has been selected. You can never be sure what other apps do or which app exactly responds to the `Intent`. – Xaver Kapeller Apr 21 '14 at 10:58
  • Is there any way to detect in the onActivityResult() mehtod that I have received a video and not an image so that I can put some conditions there? – Nitesh Kumar Apr 21 '14 at 10:58
  • The simplest check would be the file extension. A little more complex would be checking the mime type. There are a lot of solutions to this, I think there are also a few good libraries to identify file types. Depending on how sure you want to be that it is a photo you can decide what you want to do. For starters checking the file extension should be more than enough. [Here is another question that deals with this](http://stackoverflow.com/questions/13760269/android-how-to-check-if-file-is-image) – Xaver Kapeller Apr 21 '14 at 11:06

4 Answers4

2

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;
}
Nitesh Kumar
  • 5,370
  • 5
  • 37
  • 54
  • This will not work for a filename like 'this.is.my.photo.jpg'. Better to use something like: `public static String getExtensionFromPath(String imagePath) { return imagePath.substring((imagePath.lastIndexOf(".") + 1), imagePath.length()); }` – suomi35 Oct 04 '14 at 20:24
  • @suomi35 yes u r right. Even I changed that code earlier and took the last index of ".". Thanks anyway. – Nitesh Kumar Oct 05 '14 at 05:01
1

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

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
0

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()); }

suomi35
  • 1,069
  • 12
  • 11
-1

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);
Hashir Sheikh
  • 1,811
  • 1
  • 14
  • 14