0

I've coded my own file explorer and now I want to start to Implement capabilities to open various files. I have implemented Mime Types and all other things but how can I parse specific selected file to specific "viewer activity"? I know by using intents but how to receive it in that viewer app and use it. Many thanks guys :)

Lukáš Anda
  • 560
  • 1
  • 5
  • 23

1 Answers1

0

Something like this should work. Change file to whatever you want.

File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
int index = file.getName().lastIndexOf(".") + 1;
String extension = null;
if (index > 0) {
    extension = file.getName().substring(index);
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri u = Uri.parse("file://" + file.getAbsolutePath());
String mimeType = null;
if (extension != null) {
    mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
if (mimeType == null) {
    mimeType = "*/*";
}
intent.setDataAndType(u,  mimeType);
try {
    startActivity(Intent.createChooser(intent, "Open with..."));
} catch (ActivityNotFoundException e) {
    // handle the exception
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • Sorry, wrong question. I asked how to handle it from activity that receives the intent – Lukáš Anda Sep 01 '14 at 09:49
  • Ah, I misunderstood the question. Something like this should work: http://stackoverflow.com/questions/1733195/android-intent-filter-for-a-particular-file-extension – Jared Rummler Sep 01 '14 at 22:39