I want to open a file in android
. What i want to do is if the file is of type Image
then i want to open Intent Chooser
which contains applications that can view the image, and if it is of video type, then open Intent Chooser with applications that can view videos. How can i achieve this?
Asked
Active
Viewed 5,040 times
5

Unnati
- 2,441
- 17
- 37
-
go to this [http://stackoverflow.com/questions/5734678/custom-filtering-of-intent-chooser-based-on-installed-android-package-name](http://stackoverflow.com/questions/5734678/custom-filtering-of-intent-chooser-based-on-installed-android-package-name) – M D Apr 18 '14 at 06:54
-
http://stackoverflow.com/questions/8945531/pick-any-kind-file-via-an-intent-on-android – Raghunandan Apr 18 '14 at 07:01
2 Answers
14
I have found a solution. I am pasting it here so it may help other users.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file), type);
context.startActivity(intent);
-
-
Before `context.startActivity(intent);`, you should add these lines (to show `Toast` if no app found to open the file: `PackageManager manager = mActivity.getPackageManager(); List
info = manager.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY); if(info.isEmpty()) Toast.makeText(mActivity, "No app found to open this file", Toast.LENGTH_SHORT).show(); else context.startActivity(intent);` – Sufian Jul 11 '14 at 05:26 -
Shouldn't it be: file.getName().lastIndexOf(".") rather than just indexOf ? – Myoch Aug 11 '18 at 18:40
-
i am in a ClickListener how i get the context? In order to call context.startActivity(intent); – demosthenes Aug 19 '18 at 15:24
1
You should decide and know whether the file is video or image. You may do it by looking at the extension of the files.
After that you can open videos like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(result), "video/*");
startActivity(intent);
and images like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(result), "image/*");
startActivity(intent);
Android
system will open the Intent Chooser
automatically.

tasomaniac
- 10,234
- 6
- 52
- 84
-
You have misunderstood my question. I don't want to open only image or video. I want that whatever file i pass, intent chooser lists appropriate application for that file. – Unnati Apr 18 '14 at 07:29
-
Ok then you just need to change the type parameter accordingly. If it is text "text/*" it should be. You cannot do it automatically I guess. In most of the file explorers they categorize them into text, images, videos and open them accordingly. For all the files that not fit a category, they ask user what kind of a file this. – tasomaniac Apr 18 '14 at 07:33
-