当前位置: 首页 > 知识库问答 >
问题:

尝试使用FileProvider共享MP3文件时获取FileNotFoundException

萧鸿轩
2023-03-14

我正在尝试使用文件提供商共享存储在我的应用程序内部存储器的原始文件夹中的MP3文件,并允许用户使用他们选择的音频收听应用程序打开共享的MP3文件

然而,在我编写的代码中,当我尝试使用附加的内容URI作为数据执行意图时,会出现FileNotFoundException。这会导致FileProvider系统的所有其他方面出现故障。

我创建了一个MediaPlayer,让它毫无问题地打开了我想要共享的MP3文件,因此我知道该文件存在,并且我的应用程序能够访问它。

调试时,我注意到内容URI的路径似乎摆脱了/files/目录,即使它在我使用getUriFromFile的文件中:

冥想文件:/data/user/0/com。实例selfcareapplication/files/raw/breathing_冥想。mp3

冥想文件内容URI:content://com.example.selfcareapplication.fileprovider/raw/breathing_meditation.mp3

这是我清单文件中的代码:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.selfcareapplication.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

下面是我尝试使用FileProvider的代码

        Intent openMeditationFileIntent = new Intent(Intent.ACTION_VIEW);
        File meditationPath = new File(this.getFilesDir(), "raw");
        File meditationFile = new File(meditationPath, "breathing_meditation.mp3");

        Uri meditationFileContentUri = FileProvider.getUriForFile(this.getApplicationContext(), "com.example.selfcareapplication.fileprovider", meditationFile);
        openMeditationFileIntent.setData(meditationFileContentUri);
        openMeditationFileIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        if(openMeditationFileIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(openMeditationFileIntent);
        } else {
            Log.d(LOG_TAG, "Could not resolve activity");
        }

以下是我在尝试使用附加的内容URI启动intent时获得的日志:

2019-04-06 11:45:35.709 4972-4972/? D/MusicLifecycle: com.google.android.music.ui.playback.AudioPreviewActivity generated event: Activity created
2019-04-06 11:45:35.752 4972-4972/? W/MediaPlayer: Couldn't open content://com.example.selfcareapplication.fileprovider/raw/breathing_meditation.mp3: java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
2019-04-06 11:45:35.761 1844-2504/? W/ActivityManager: Permission Denial: opening provider android.support.v4.content.FileProvider from (null) (pid=1694, uid=1013) that is not exported from UID 10092
2019-04-06 11:45:35.766 1694-1776/? E/MediaPlayerService: Couldn't open fd for content://com.example.selfcareapplication.fileprovider/raw/breathing_meditation.mp3
2019-04-06 11:45:35.766 4972-4972/? E/MediaPlayerNative: Unable to create media player
2019-04-06 11:45:35.768 4972-4972/? D/AudioPreviewActivity: Failed to open file: java.io.IOException: setDataSource failed.: status=0x80000000
2019-04-06 11:45:35.810 1584-1626/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 376832
2019-04-06 11:45:35.818 1584-1626/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 376832
2019-04-06 11:45:35.820 1584-1626/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 376832
2019-04-06 11:45:35.847 4972-4999/? D/EGL_emulation: eglMakeCurrent: 0xea7c5e60: ver 2 0 (tinfo 0xea7b1a10)
2019-04-06 11:45:35.901 4972-4972/? D/MusicLifecycle: com.google.android.music.ui.MusicUrlHandlerActivity generated event: Activity stopped
2019-04-06 11:45:35.931 4972-4972/? D/MusicLifecycle: com.google.android.music.ui.MusicUrlHandlerActivity generated event: Activity destroyed
2019-04-06 11:45:35.939 1684-2170/? D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
2019-04-06 11:45:35.941 1684-2170/? D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
2019-04-06 11:45:35.943 1684-2170/? D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
2019-04-06 11:45:35.944 1684-2170/? D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
2019-04-06 11:45:35.968 1844-3656/? I/MediaFocusControl: abandonAudioFocus() from uid/pid 10069/4972 clientId=android.media.AudioManager@32b5d4dcom.google.android.music.ui.playback.AudioPreviewActivity@6726002
2019-04-06 11:45:35.969 4972-4972/? D/MusicLifecycle: com.google.android.music.ui.playback.AudioPreviewActivity generated event: Activity destroyed
2019-04-06 11:45:36.091 1844-2247/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@9a951f4)
2019-04-06 11:45:36.092 1844-1857/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@9a951f4)
2019-04-06 11:45:36.175 4775-4775/? E/GoogleTagManager: Invalid macro: _gtm.loadEventEnabled
2019-04-06 11:45:36.319 1844-1857/? E/memtrack: Couldn't load memtrack module
2019-04-06 11:45:36.319 1844-1857/? W/android.os.Debug: failed to get memory consumption info: -1
2019-04-06 11:45:36.419 4775-5006/? E/GoogleTagManager: Invalid macro: _gtm.loadEventEnabled
2019-04-06 11:45:36.487 4775-4829/? I/MediaStoreImporter: Update: incremental Success: true Added music: 0 Updated music: 0 Deleted music: 0 Created playlists: 0 Updated playlists: 0 Deleted playlists: 0 Inserted playlist items: 0 Deleted playlist items: 0 Removed orphaned playlist items: 0 Total item count: 0
2019-04-06 11:45:36.491 4775-4775/? D/MusicLifecycle: com.google.android.music.download.EncapsulatedBroadcastReceiver$1 generated event: Broadcast received with context com.google.android.music.ui.PhoneMusicApplication@a0c548d and intent Intent { act=com.google.android.music.IMPORT_COMPLETE flg=0x10 }
2019-04-06 11:45:36.609 4775-5008/? E/GoogleTagManager: Invalid macro: _gtm.loadEventEnabled
2019-04-06 11:45:36.637 4775-5000/? E/GoogleTagManager: Unsupported Value Escaping: 3
2019-04-06 11:45:36.752 1844-1955/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
2019-04-06 11:45:36.684 4775-5000/? E/GoogleTagManager: Unsupported Value Escaping: 3

共有1个答案

徐涵亮
2023-03-14

我在/res中创建了/raw目录并复制了。mp3文件在那里

那是在你的开发机器上。

如果你不介意我问的话,创建原始/目录和创建原始资源之间到底有什么区别?

不同之处在于,getFilesDir()是指Android设备上的位置。您创建的原始目录位于开发机器上。

将文件移动到/res/raw文件夹中不会将其变成可以使用R.raw引用的原始资源吗?

是的。但是,这不是您的代码正在做的事情。您的代码正试图使用File引用它。原始资源是您的开发机器上的文件。它们不是设备上的文件。

您需要将原始资源(getResources(). openRawResources()等)复制到文件中,然后您可以使用FileProvider共享该文件。

 类似资料:
  • 但是,执行这一行给了我(为了保护无辜者而改变了名字): 应用程序A在清单文件中有以下内容: filePaths.xml具有: 文件最终的实际路径是: 在这一点上,我被难住了。我不知道为什么不能授予我在同一个应用程序中创建的文件的权限。所以我的问题是:为什么我会得到这个异常,我如何才能成功地授予应用程序B的权限?

  • 跟随https://developer.android.com/training/secure-file-sharing/index.html,并能够共享文件在内部目录(/data/data/pack/files/xxx/)的应用程序到客户端应用程序使用file提供程序。 如何将资产文件夹(而不是内部目录)中的文件共享到客户端应用程序。 谢谢

  • 我正在尝试使用内容提供程序共享我的内部日志文件。我有以下

  • 我正在尝试传递一个位于我的应用程序的res/raw目录中的图像以及共享意图。 我遵循了FileProvider文档中描述的流程,下面是我的代码: Androidanifest.xml 我的活动中的代码: 由于无法访问我在其他应用程序中获取的文件,因此上述操作无效: java.io.FileNotFoundException:FILE_PATH:打开失败: EACCES(权限拒绝) 知道我做错了什么

  • 我看到了很多代码和示例,但我的应用程序还不能正常工作,我想在文件夹,如存储/模拟/0/Android/data/ir。esfandune。mohsen/files/Download/cnt\u imags/images/n1/n1(18)。jpg,我在清单中定义提供者: 和XML: 我的代码是: 但是当发送到其他应用程序时,不要分享任何东西

  • 从外部应用程序,我想这样做,以访问文件提供商的内部存储上的文件: 是我在其他(fileprovider)应用程序的清单xml中指定的权限字符串。 和: 该文件存在于内部FP的存储器中(files)/path/to/file下。txt 但我总是得到IllegalArgumentException no root等。这不是默认的Android FileProvider的用途吗?我是否误解了它的用途?例