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();
            }
相关推荐
yewq-cn9 分钟前
AOSP 下载
android
cch891832 分钟前
Laravel vs ThinkPHP:PHP框架终极对决
android·php·laravel
米码收割机1 小时前
【Android】基于安卓app的汽车租赁管理系统(源码+部署方式+论文)[独一无二]
android·汽车
流星雨在线1 小时前
安卓使用 Startup 管理三方 SDK 初始化
android·startup
jwn9991 小时前
Laravel3.x:PHP框架的经典里程碑
android
lishutong10061 小时前
基于 Perfetto 与 AI 的 Android 性能自动化诊断方案
android·人工智能·自动化
REDcker1 小时前
Android Bionic Libc 原理与实现综述
android·c++·c·ndk·native·bionic
葱段1 小时前
Flutter 设置Android System Navigation/Status Bar背景色
android·flutter
半条-咸鱼2 小时前
如何通过 ADB 连接安卓设备(USB + 无线 TCP/IP)
android·adb
vonlycn2 小时前
Android Studio 5.3.3 新项目编译报错解决
android·ide·android studio