How to select a GIF file from the gallery?
I am new to android development. I am currently working on an application where I need to select a GIF file from the Gallery. I am using this below code
image_gif.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/gif");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_GIF_REQUEST);
But using this code my gallery is showing all file types (.jpeg, .png, .mp4, etc.). And whenever I select one of them, my application crashes and throws badTokenException. Thanks in advance for your help.
+3
source to share
1 answer
List all the gifs in your SD card with the code below and then let the pic user specify the gif from the list.
private List<String> getListOfFiles(String path) {
File files = new File(path);
FileFilter filter = new FileFilter() {
private final List<String> exts = Arrays.asList("gif");
@Override
public boolean accept(File pathname) {
String ext;
String path = pathname.getPath();
ext = path.substring(path.lastIndexOf(".") + 1);
return exts.contains(ext);
}
};
final File [] filesFound = files.listFiles(filter);
List<String> list = new ArrayList<String>();
if (filesFound != null && filesFound.length > 0) {
for (File file : filesFound) {
list.add(file.getName());
}
}
return list;
}
-1
source to share