android打开与分享文件

Posted 楠之枫雪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android打开与分享文件相关的知识,希望对你有一定的参考价值。

  • 创建fileProvider

    <application>//放在application中
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.h3c.sharedemo.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths"
               />
        </provider>
    </application>
  • 创建filepaths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path
        name="files-path"
        path="." />
    <cache-path
        name="cache-path"
        path="." />
    <external-path
        name="external_storage_root"
        path="." />
    <external-files-path
        name="external_file_path"
        path="." />
    <external-cache-path
        name="external_cache_path"
        path="." />
    <root-path
        name="root-path"
        path="" />
</paths>
  • 通过intent分享
   Intent intent =new Intent(Intent.ACTION_SEND);
        intent.setType(FileUtil.getMIMEType(picFile));
        Uri contentUri = FileProvider.getUriForFile(context, picFile);
        intent.putExtra(Intent.EXTRA_STREAM,contentUri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        ComponentName comp = new ComponentName(packageName, activityName);
        intent.setComponent(comp);//这里指导打开的应用,不需要选择,因为我自定义应用选择器
        context.startActivity(intent);
  • 通过intent查看
 Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.setAction(Intent.ACTION_VIEW);
        String type = FileUtil.getMIMEType(picFile);
        try 
            Log.e("getMIMEType", type);
            intent.setDataAndType(FileProvider.getUriForFile(context, picFile), type);
            context.startActivity(intent);
         catch (Exception e) 
            Log.e(tag, e.toString());
        

FileProvider:

public class FileProvider 

    public static Uri getUriForFile(Context context, File file) 
        Uri fileUri;
        if (Build.VERSION.SDK_INT >= 24) 
            fileUri = getUriForFile24(context, file);
         else 
            fileUri = Uri.fromFile(file);
        
        return fileUri;
    

    public static Uri getUriForFile24(Context context, File file) 
        Uri fileUri = androidx.core.content.FileProvider.getUriForFile(context,
                "com.h3c.sharedemo.fileprovider",
                file);
        return fileUri;
    


MineType获取:

 /**
     * @param file file
     * @return getMIMEType
     */
    public static String getMIMEType(File file) 
        String type = "*/*";
        String name = file.getName();
        int index = name.lastIndexOf('.');
        if (index < 0) 
            return type;
        
        String end = name.substring(index).toLowerCase();
        if (TextUtils.isEmpty(end)) return type;
        for (String[] strings : MIME_MapTable) 
            if (end.equals(strings[0])) type = strings[1];
        
        return type;
    

    private static final String[][] MIME_MapTable = 
            ".flv", "video/flv",
            ".3gp", "video/3gpp",
            ".apk", "application/vnd.android.package-archive",
            ".asf", "video/x-ms-asf",
            ".avi", "video/x-msvideo",
            ".bin", "application/octet-stream",
            ".bmp", "image/bmp",
            ".c", "text/plain",
            ".class", "application/octet-stream",
            ".conf", "text/plain",
            ".cpp", "text/plain",
            ".doc", "application/msword",
            ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            ".xls", "application/vnd.ms-excel",
            ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            ".exe", "application/octet-stream",
            ".gif", "image/gif",
            ".gtar", "application/x-gtar",
            ".gz", "application/x-gzip",
            ".h", "text/plain",
            ".htm", "text/html",
            ".html", "text/html",
            ".jar", "application/java-archive",
            ".java", "text/plain",
            ".jpeg", "image/jpeg",
            ".jpg", "image/jpeg",
            ".js", "application/x-javascript",
            ".log", "text/plain",
            ".m3u", "audio/x-mpegurl",
            ".m4a", "audio/mp4a-latm",
            ".m4b", "audio/mp4a-latm",
            ".m4p", "audio/mp4a-latm",
            ".m4u", "video/vnd.mpegurl",
            ".m4v", "video/x-m4v",
            ".mov", "video/quicktime",
            ".mp2", "audio/x-mpeg",
            ".mp3", "audio/x-mpeg",
            ".mp4", "video/mp4",
            ".mpc", "application/vnd.mpohun.certificate",
            ".mpe", "video/mpeg",
            ".mpeg", "video/mpeg",
            ".mpg", "video/mpeg",
            ".mpg4", "video/mp4",
            ".mpga", "audio/mpeg",
            ".msg", "application/vnd.ms-outlook",
            ".ogg", "audio/ogg",
            ".pdf", "application/pdf",
            ".png", "image/png",
            ".pps", "application/vnd.ms-powerpoint",
            ".ppt", "application/vnd.ms-powerpoint",
            ".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation",
            ".prop", "text/plain",
            ".rc", "text/plain",
            ".rmvb", "audio/x-pn-realaudio",
            ".rtf", "application/rtf",
            ".sh", "text/plain",
            ".tar", "application/x-tar",
            ".tgz", "application/x-compressed",
            ".txt", "text/plain",
            ".wav", "audio/x-wav",
            ".wma", "audio/x-ms-wma",
            ".wmv", "audio/x-ms-wmv",
            ".wps", "application/vnd.ms-works",
            ".xml", "text/plain",
            ".z", "application/x-compress",
            ".zip", "application/x-zip-compressed",
            "", "*/*"
    ;
  • 注意。如果Android10及以上,必须在application中添加下面一句,否则将无法读取到文件数据
 android:requestLegacyExternalStorage="true"

以上是关于android打开与分享文件的主要内容,如果未能解决你的问题,请参考以下文章

android打开与分享文件

android打开与分享文件

基于android的学院通知与文件分享系统APP(ssm+uinapp+Mysql)

Appium环境部署(二)——Android_SDK的下载、安装与配置

Android配置文件分享和JSON数据生成与解析

Android分享笔记 Android的webview加载本地html本apk内html和远程URL