android 打开预览不同类型文件

在Android系统中,由于权限限制,通过Intent.getData()打开文件可能会遇到错误。解决办法是在AndroidManifest.xml中配置FileProvider,并在res/xml目录下创建file_paths.xml定义文件路径。使用<files-path/>、<external-path/>和<cache-path/>定义根目录,然后通过代码传入文件路径以安全预览文件。
摘要由CSDN通过智能技术生成

exposed beyond app through Intent.getData() 

调用手机app打开不同文件是会遇到上面问题,这是android从××版本开始要求访问权限,并要求通过URI访问文件。

先在AndroidManifest.xml文件添加fileprovider

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="app包名.fileprovider"
    android:exported="false"

    android:grantUriPermissions="true">
    <meta-data
       android:name="android.support.FILE_PROVIDER_PATHS"
       android:resource="@xml/file_paths"
    />
</provider>

//exported:要求必须为false,为true则会报安全异常。
//grantUriPermissions:true,表示授予 URI 临时访问权限。

在res资源目录下新建一个xml目录放入file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="Android/data/com.data.cloudminds.disk/" name="files_root"/>

    <external-path path="." name="external_storage_root"/>

</paths>

上面配置好了现在上代码打开文件,只有传入文件路径即可

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MineType {
    static String TAG = "MineType";
    private static final String auther = "app包名.fileprovider";//与AndroidManifest.xml 保存一只

    public static String getMIMETypeString(File file) {
        String type = "*/*";
        String strings[] = file.getPath().split("/");
        String fName = strings[strings.length - 1];
        //获取后缀名前的分隔符"."在fName中的位置。
        int dotIndex = fName.lastIndexOf(".");
        if (dotIndex < 0) {
            return type;
        }
        dotIndex++;
        /* 获取文件的后缀名 */
        String end = fName.substring(dotIndex, fName.length()).toLowerCase();
        Log.d(TAG, "fName = " + fName + " dotIndex = " + dotIndex + " end = " + end);
        if (TextUtils.isEmpty(end)) return type;
        //在MIME和文件类型的匹配表中找到对应的MIME类型。
        for (int i = 0; i < MIME_MapTable.length; i++) {
            if (end.equals(MIME_MapTable[i][0]))
                type = MIME_MapTable[i][1];
        }
        return type;
    }

    /**
     * 打开文件
     *
     * @param file
     */
    public static void openFile(Context context, File file) {
        //Uri uri = Uri.parse("file://"+file.getAbsolutePath());
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //设置intent的Action属性
        intent.setAction(Intent.ACTION_VIEW);
        //获取文件file的MIME类型
        String type = getMIMETypeString(file);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        Uri uri = FileProvider.getUriForFile(context, auther,file);
        //设置intent的data和Type属性。
        intent.setDataAndType(uri, type);
        //跳转
        context.startActivity(intent);
    }

    public static final String[][] MIME_MapTable = {
            //{后缀名,MIME类型}
            {"pbm", "image/x-portable-bitmap"},
            {"pcx", "image/x-pcx"},
            {"nbmp", "image/nbmp"},
            {"pda", "image/x-pda"},
            {"pgm", "image/x-portable-graymap"},
            {"pict", "image/x-pict"},
            {"png", "image/png"},
            {"pnm", "image/x-portable-anymap"},
            {"pnz", "image/png"},
            {"ppm", "image/x-portable-pixmap"},
            {"nokia-op-logo", "image/vnd.nok-oplogo-color"},
            {"qti", "image/x-quicktime"},
            {"qtif", "image/x-quicktime"},
            {"ras", "image/x-cmu-raster"},
            {"rf", "image/vnd.rn-realflash"},
            {"rp", "image/vnd.rn-realpix"},
            {"rgb", "image/x-rgb"},
            {"si9", "image/vnd.lgtwap.sis"},
            {"si7", "image/vnd.stiwap.sis"},
            {"svf", "image/vnd"},
            {"svg", "image/svg-xml"},
            {"svh", "image/svh"},
            {"si6", "image/si6"},
            {"tif", "image/tiff"},
            {"tiff", "image/tiff"},
            {"toy", "image/toy"},
            {"wbmp", "image/vnd.wap.wbmp"},
            {"wi", "image/wavelet"},
            {"wpng", "image/x-up-wpng"},
            {"xbm", "image/x-xbit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值