load Document In Reader by file extension using Intent - Android Intent

Android examples for Intent:Open App

Description

load Document In Reader by file extension using Intent

Demo Code


//package com.java2s;
import java.io.File;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;

public class Main {
    public static Intent loadDocInReader(File file)
            throws ActivityNotFoundException, Exception {
        Uri path = Uri.fromFile(file);/*from w w  w.ja va  2  s  . c o  m*/
        Intent it = new Intent(Intent.ACTION_VIEW);
        it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        if (file.getName().endsWith(".pdf")
                || file.getName().endsWith(".PDF"))
            it.setDataAndType(path, "application/pdf");
        else if (file.getName().endsWith(".WAV")
                || file.getName().endsWith(".wav"))
            it.setDataAndType(path, "audio/vnd.wave");
        else if (file.getName().endsWith(".MP3")
                || file.getName().endsWith(".mp3"))
            it.setDataAndType(path, "audio/mpeg");
        else if (file.getName().endsWith(".DOC")
                || file.getName().endsWith(".doc"))
            it.setDataAndType(path, "application/msword");
        else if (file.getName().endsWith(".DOCX")
                || file.getName().endsWith(".docx"))
            it.setDataAndType(path,
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        else if (file.getName().endsWith(".PPT")
                || file.getName().endsWith(".ppt"))
            it.setDataAndType(path, "application/vnd.ms-powerpoint");
        else if (file.getName().endsWith(".PPTX")
                || file.getName().endsWith(".pptx"))
            it.setDataAndType(path,
                    "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        else if (file.getName().endsWith(".PPSX")
                || file.getName().endsWith(".ppsx"))
            it.setDataAndType(path,
                    "application/vnd.openxmlformats-officedocument.presentationml.slideshow");
        else if (file.getName().endsWith(".JPG")
                || file.getName().endsWith(".jpg")
                || file.getName().endsWith(".JPEG")
                || file.getName().endsWith(".jpeg"))
            it.setDataAndType(path, "image/jpeg");
        else if (file.getName().endsWith(".PNG")
                || file.getName().endsWith(".png"))
            it.setDataAndType(path, "image/png");
        else if (file.getName().endsWith(".TXT")
                || file.getName().endsWith(".txt"))
            it.setDataAndType(path, "text/plain");
        else if (file.getName().endsWith(".MP4")
                || file.getName().endsWith(".mp4"))
            it.setDataAndType(path, "video/mp4");
        else
            it.setDataAndType(path, "text/plain");
        return it;
    }

    public static Intent loadDocInReader(File file, String mimeType)
            throws ActivityNotFoundException, Exception {
        Uri path = Uri.fromFile(file);
        Intent it = new Intent(Intent.ACTION_VIEW);
        it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        it.setDataAndType(path, mimeType);
        return it;
    }
}

Related Tutorials