Android将Uri转为路径字符串(适配安卓全版本)并使用第三方应用打开文件(适配Android7.0+)

做这个功能时在网上找了无数篇例子,有些方法是有问题的,故自己写一篇完整实现的总结,作备忘也作案例。顺便说一句,Android对存储权限的给予真的越来越严格

目录

1.Uri转为路径String以获得文件名 

2.获取文件后缀名

3.通过后缀名获取文件MIME类型 

4.设置Intent的Uri与权限并打开文件 


1.Uri转为路径String以获得文件名 

有一种方法是使用ContentResolver对象查询Uri对应的Cursor对象然后获取文件名,这个方法只适用于文件位于应用公共目录,在此不多赘述。我这里的需求是用第三方应用打开某个文件,本身有目标路径。如果要把Uri转成路径来获得文件名就要用到convertUriToFilePath方法,类似方法网上杂七杂八的好多,大部分都用不了或者只能获取图片路径。我分享一个自己一直用的,使用时传入两个参数:ContextUri

public static String convertUriToFilePath(final Context context, final Uri uri) {
        String path = null;
        if (DocumentsContract.isDocumentUri(context, uri)) {
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    path = Environment.getExternalStorageDirectory() + "/" + split[1];
                }
            } else if (isDownloadsDocument(uri)) {
                final String id = DocumentsContract.getDocumentId(uri);

                if (!TextUtils.isEmpty(id)) {
                    if (id.startsWith("raw:")) {
                        return id.replaceFirst("raw:", "");
                    }
                }

                final Uri contentUri = ContentUris
                        .withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                path = getDataColumn(context, contentUri, null, null);
            } else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = MediaStore.Audio.Media._ID + "=?";
                final String[] selectionArgs = new String[]{
                        split[1]
                };

                path = getDataColumn(context, contentUri, selection, selectionArgs);
            }
        } else if (ContentResolver.SCHEME_CONTENT.equalsIgnoreCase(uri.getScheme())) {
            path = getDataColumn(context, uri, null, null);
        } else if (ContentResolver.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) {
            path = uri.getPath();
        }

        if (path != null) {
            try {
                return URLDecoder.decode(path, "UTF-8");
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }

以下是使用示例:

Uri uri = data.getData();  //data是传入的Intent。你可以将data.getData()替换为你需要的其他uri
String filePath = convertUriToFilePath(getApplicationContext(), uri);  //调用方法

2.获取文件后缀名

这里我使用File中的file.getName()来获取文件名,再用截取字符串获取文件后缀名

File file = new File(filePath);
String fName = file.getName();
String end = "";
int dotIndex = fName.lastIndexOf(".");
if (dotIndex > 0) {
    //获取文件的后缀名
    end = fName.substring(dotIndex, fName.length()).toLowerCase(Locale.getDefault());
}

3.通过后缀名获取文件MIME类型 

网上有人专门为了获得MIME类型做了个MimeMap,其实有自带的android.webkit.MimeTypeMap中的方法

String mimetype = "";
mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(end);

4.设置Intent的Uri与权限并打开文件 

如果你是有Uri的,那你就跳过这里直接看Intent的做法。如果你跳过了第一步,像我一样是从路径来打开的,那么你需要先得到Uri。SDK23以上需要拥有fileProvider授权来获取uri。

在AndroidManifest.xml中添加provider(注意将“你应用的包名”更改为你的实际包名)

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="你应用的包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/file_paths" />
</provider>

在res/xml中(没有xml文件夹就新建一个)新建file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="." />
    <root-path
        name="external_files"
        path="" />
</paths>

将File加载为Uri(注意将“你应用的包名”更改为你的实际包名)

Uri uri;
if (Build.VERSION.SDK_INT > 23) {
    //Android 7.0之后
    uri = FileProvider.getUriForFile(context, "你应用的包名.fileprovider", file);
    } else {
    uri = Uri.fromFile(file);
}

 新建Intent并启动

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uriForFile, mimetype);

//检查context是否不是Activity的实例
if (!(context instanceof Activity)) {
    //若不是Activity则将活动作为新任务栈的一部分启动
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //  最重要的一句,网上很多版本没有。该语句授予Intent的组件临时读取URI的权限

startActivity(intent);

解释Intent.FLAG_ACTIVITY_NEW_TASK:当使用Intent启动一个活动时,如果当前Context是一个服务或其他类型的Context,那么需要添加FLAG_ACTIVITY_NEW_TASK标志。因为只有活动的Context才能直接启动新的活动,而其他类型的上下文无法直接启动活动。添加FLAG_ACTIVITY_NEW_TASK标志可以告诉系统将这个活动作为新任务栈的一部分启动,从而绕过这个限制。

注意,务必加上intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION),否则其他应用没有权限读你的Uri。但是目前已知WPS和MT管理器可以绕开此限制,不知道他们是如何绕开的。

本文完

若对文章内容出现疑问,欢迎私信交流,欢迎共同进步

致谢以下文章:

Android调用外部应用打开文件_android打开文件-CSDN博客

【基础笔记】Android如何用第三方软件打开文件 - 简书

解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/_file:///storage/emulated/0/hwbrowser/.picview_temp-CSDN博客

  • 23
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android 11 ,访问外部存储需要申请 `MANAGE_EXTERNAL_STORAGE` 权限。如果没有申请该权限,会导致访问外部存储时出现错误。 可以参考以下代码,在 `AndroidManifest.xml` 文件申请该权限: ```xml <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> ``` 同时,在运行时也需要动态申请该权限。 以下是将 U 盘的 Uri 换为路径的示例代码: ```java public class FileUtils { public static String getUsbDiskPath(Context context) { String usbDiskPath = null; File[] dirs = ContextCompat.getExternalFilesDirs(context, null); for (File dir : dirs) { if (dir != null && dir.exists() && dir.isDirectory() && dir.canWrite()) { String dirPath = dir.getAbsolutePath(); if (dirPath.startsWith("/storage/")) { // 内置存储 continue; } else { // 外置存储 usbDiskPath = dirPath; break; } } } return usbDiskPath; } public static String getRealPathFromUri(Context context, Uri uri) { String realPath = null; if (DocumentsContract.isDocumentUri(context, uri)) { if (isExternalStorageDocument(uri)) { // 外部存储 String docId = DocumentsContract.getDocumentId(uri); String[] split = docId.split(":"); String type = split[0]; if ("primary".equalsIgnoreCase(type)) { realPath = Environment.getExternalStorageDirectory() + "/" + split[1]; } else { String usbDiskPath = getUsbDiskPath(context); if (usbDiskPath != null) { realPath = usbDiskPath + "/" + split[1]; } } } else if (isDownloadsDocument(uri)) { // 下载目录 String id = DocumentsContract.getDocumentId(uri); Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); realPath = getDataColumn(context, contentUri, null, null); } else if (isMediaDocument(uri)) { // 媒体库 String docId = DocumentsContract.getDocumentId(uri); String[] split = docId.split(":"); String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } String selection = "_id=?"; String[] selectionArgs = new String[]{split[1]}; realPath = getDataColumn(context, contentUri, selection, selectionArgs); } } else if (ContentResolver.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) { // 直接从文件获取 realPath = uri.getPath(); } return realPath; } private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; String column = "_data"; String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } return null; } private static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } private static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } private static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } } ``` 该工具类的 `getRealPathFromUri` 方法可以将 U 盘的 Uri 换为路径。具体实现是根据 Uri 的不同类型,采用不同的方式获取路径

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值