Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 Intent 打開各類檔案:PDF、Office、音訊與影像一把抓

編輯:關於 Android 程式設計

Android 沒有內建的文件檢視器,但系統可以幫你找到有安裝的第三方 App。關鍵在於 MIME type 要填對,以及 URI 要交得出去。

核心概念

做法都是同一套:建立 ACTION_VIEW 意圖,指定檔案 URI,再告訴系統這是什麼類型。系統拿 MIME type 去比對已安裝程式的意圖過濾器,挑出能處理的 App 交給使用者選擇。

各種檔案的 Intent 工廠

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import java.io.File;

/**
 * 自訂 Intent 工具類,可用於取得開啟以下檔案的 Intent:
 * PDF、PPT、WORD、EXCEL、CHM、HTML、TEXT、AUDIO、VIDEO、IMAGE
 */
public class MyIntent {

    // 開啟 HTML 檔案
    public static Intent getHtmlFileIntent(String param) {
        Uri uri = Uri.parse(param).buildUpon()
                .encodedAuthority("com.android.htmlfileprovider")
                .scheme("content").encodedPath(param).build();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "text/html");
        return intent;
    }

    // 開啟圖片
    public static Intent getImageFileIntent(String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "image/*");
        return intent;
    }

    // 開啟 PDF
    public static Intent getPdfFileIntent(String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "application/pdf");
        return intent;
    }

    // 開啟純文字,paramBoolean 決定使用 parse 還是 fromFile
    public static Intent getTextFileIntent(String param, boolean paramBoolean) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (paramBoolean) {
            intent.setDataAndType(Uri.parse(param), "text/plain");
        } else {
            intent.setDataAndType(Uri.fromFile(new File(param)), "text/plain");
        }
        return intent;
    }

    // 開啟音訊
    public static Intent getAudioFileIntent(String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "audio/*");
        return intent;
    }

    // 開啟影片
    public static Intent getVideoFileIntent(String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "video/*");
        return intent;
    }

    // 開啟 CHM
    public static Intent getChmFileIntent(String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "application/x-chm");
        return intent;
    }

    // 開啟 Word
    public static Intent getWordFileIntent(String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "application/msword");
        return intent;
    }

    // 開啟 Excel
    public static Intent getExcelFileIntent(String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "application/vnd.ms-excel");
        return intent;
    }

    // 開啟 PPT
    public static Intent getPptFileIntent(String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        return intent;
    }
}

使用範例

// 這個寫法行不通:PDF.apk 無法存取其他 APK 內 assets 的資源
// Intent it = getPdfFileIntent("file:///android_asset/helphelp.pdf");

// 以下皆可正常開啟
Intent it = getHtmlFileIntent("/sdcard/tutorial.html");        // SD 卡根目錄
it = getHtmlFileIntent("/system/etc/tutorial.html");           // 系統內部 etc 目錄
it = getPdfFileIntent("/system/etc/helphelp.pdf");
it = getWordFileIntent("/system/etc/help.doc");
it = getExcelFileIntent("/mnt/sdcard/Book1.xls");
it = getPptFileIntent("/mnt/sdcard/download/Android_PPT.ppt");
it = getVideoFileIntent("/mnt/sdcard/ice.avi");
it = getAudioFileIntent("/mnt/sdcard/ren.mp3");
it = getImageFileIntent("/mnt/sdcard/images/001041580.jpg");
it = getTextFileIntent("/mnt/sdcard/hello.txt", false);
startActivity(it);

三個實務提醒

第一,assets 內的檔案沒有對外 URI,交給外部 App 一定打不開,這與影片那個案例是同一個原因。第二,從 Android 7.0 起,直接傳 file:// URI 給其他 App 會觸發 FileUriExposedException,必須改用 FileProvider 產生 content:// URI 並授予讀取權限。第三,若裝置上沒有可處理該 MIME type 的 App,startActivity 會拋出 ActivityNotFoundException,記得包上 try-catch 並給使用者提示。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved