android - 使用FileProvider共享/data/user/0/目录中的音频文件

标签 android audio android-intent android-fileprovider android-sharing

我正在使用FileProvider API共享实际存储在内部存储器中的内容。
以下是我的XML配置,该配置与Manifiedt文件中配置的提供程序链接。

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="xs_audio" path="/audio/records"/>
</paths>
我用来共享的代码如下:
    private final static String FILE_PROVIDER = BuildConfig.APPLICATION_ID + ".fileprovider";
        
    private String testPackageAppHaveAccess = "com.whats";
            public static void shareDocument(Activity activity, CallRecordData data) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND_MULTIPLE);
                intent.putExtra(Intent.EXTRA_SUBJECT, "Record File");
                intent.setType("audio/*");
        
                ArrayList<Uri> files = new ArrayList<>();
                //for (AudioModelObj image : data.getDocuments()) {
                    files.add(getImageUri(activity, data.getFile()));
                //}
        
                activity.grantUriPermission(testPackageAppHaveAccess, files.get(0), Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
                activity.startActivity(Intent.createChooser(intent, "Share Audio File."));
            }
        
            private static Uri getImageUri(Activity activity, String audioURI) {
                if (Build.VERSION.SDK_INT < 24) {
                    return Uri.parse(audioURI);
                } else {
                    URI uri = URI.create(audioURI);
                    File file = new File(uri);
                    return FileProvider.getUriForFile(activity, FILE_PROVIDER, file);
                }
            }
}
但是在使用应用启动时,它没有附加任何内容。如果是gmail,则说“无法附加空文件”。我正在显示文件列表并正在播放时,文件已确认可用。
供引用:从getImageUri(..)生成的Uri是

/data/user/0/com.xl.cl.debug/cache/audio/records/17-10-17_170728_abc_.wav


有什么建议我做错了吗?

最佳答案

  • <files-path>已经指向某些设备上的/data/user/0/com.xl.cl.debug
  • <cache-path>是您应该使用的,只用感兴趣的子目录(path)替换audio/records,消除了/data/user/0/com.xl.cl.debug/cache
  • 您正在调用grantUriPermission(),其中第一个参数不是包
  • 您正在调用grantUriPermission(),其中第一个参数不是标识要尝试向其授予权限的应用程序的包
  • 您没有在FLAG_GRANT_READ_URI_PERMISSION中添加Intent,这是一种典型的说法:“此Uri的接收者应该可以读取此Intent中的Intent”(尽管ACTION_SEND_MULTIPLE在这里可能需要更多工作,因为我没有玩了那么多)
  • 人类历史上没有文件系统路径以content:/开头,因此请摆脱掉
  • 调用new File()并提供一个不是文件系统路径的值将无法正常工作

  • 问题可能比这些更多,但这应该可以帮助您入门

    关于android - 使用FileProvider共享/data/user/0/目录中的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46789824/

    相关文章:

    java - Android gridview 不维护元素顺序(除非牺牲性能)

    javascript - 钛金属日期

    c# - 使用 C# 控制媒体和应用程序卷

    audio - praat - 删除段

    java - 如何重置createChooser Intent 中的数据

    java - 有没有办法使用 ACTION_SEND 在 google plus 上发布多张图片

    android - 如何正确跟踪播放位置? [Android 媒体广播通知 Spotify API]

    android - 如何获取名称奇怪的文件的 MIME 类型?

    android - Android TextView 上的最大行长度

    javascript - 如何在没有任何 html5 功能的情况下通过 javascript 播放 mp3 文件?