Android:文件管理:打开文件意图

三步走:

一、先在AndroidManifest.xml声明provider:

html 复制代码
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

二、创建@xml/provider_paths

html 复制代码
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

三、打开文件:

java 复制代码
            Intent intent = new Intent(Intent.ACTION_VIEW);
            File file = new File("/sdcard/Music/音乐文件.mp3");
            // "com.example.myapplication.FileProvider"对应AndroidManifest.xml声明的android:authorities="${applicationId}.FileProvider"
            Uri uri = FileProvider.getUriForFile(getApplicationContext(), "com.example.myapplication.FileProvider", file);
            // 其他类型根据你实际的文件来
            intent.setDataAndType(uri, "audio/mp3");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(this, "没有找到播放音频的应用", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
相关推荐
Android系统攻城狮2 分钟前
Android tinyalsa深度解析之pcm_params_set_min调用流程与实战(一百六十九)
android·pcm·tinyalsa·音频进阶
高梦轩9 分钟前
MySQL 主从复制 + 读写分离
android·数据库
cch891833 分钟前
PHP vs C++:10倍性能差距的编程语言对决
android·java·开发语言
cnnews34 分钟前
Termux中安装python包
android·linux·开发语言·python·安卓·termux
00后程序员张7 小时前
从审核被拒到稳定过审,iOS 上架技术优化
android·ios·小程序·https·uni-app·iphone·webview
不会写DN10 小时前
PHP 中的文件读写与上传
android·开发语言·php
冬奇Lab12 小时前
Android 15音频子系统(七):音量控制系统深度解析
android·音视频开发
sg_knight16 小时前
CentOS 裸机实操:5分钟完成 MinIO 单机部署与公网访问
linux·python·centos·文件管理·minio·ftp·oss
方白羽16 小时前
Android NFC 功能集成-读卡器模式
android·app·客户端
进击的cc16 小时前
Android Kotlin:委托属性深度解析
android·kotlin