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();
            }
相关推荐
Jomurphys18 小时前
Compose 调用 - 震动 LocalHapticFeedback
android·compose
非凡ghost18 小时前
小X分身APP(手机分身类工具)
android·windows·学习·智能手机·软件需求
erqi21 小时前
Compose你入门吧
android
q***765621 小时前
MySQL 中如何进行 SQL 调优
android·sql·mysql
zhanglinping6191 天前
MySQL——内置函数
android·数据库·mysql
m***78741 天前
mysql之字符串函数
android·数据库·mysql
w***71101 天前
MySQL 事务的操作和四大特性
android·数据库·mysql
松叶似针1 天前
Flutter三方库适配OpenHarmony【secure_application】— Android 端 FLAG_SECURE 实现分析
android·flutter
cjl_8520081 天前
[MySQL] MySQL复合查询(多表查询、子查询)
android·mysql·adb
stevenzqzq1 天前
Barrier的用法
android