MediaPlayer - java.io.FileNotFoundException: 没有内容提供者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46080969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:03:02  来源:igfitidea点击:

MediaPlayer - java.io.FileNotFoundException: No content provider

javaandroidexceptionandroid-mediaplayer

提问by Andrej Korowacki

I've been writing an mp3 player for quite some time, but for some reason this exception:

我写一个 mp3 播放器已经有一段时间了,但由于某种原因,这个例外:

W/MediaPlayer: Couldn't open /storage/emulated/0/Music/generic music file.mp3: java.io.FileNotFoundException: No content provider: /storage/emulated/0/Music/generic music file.mp3

W/MediaPlayer:无法打开/storage/emulated/0/Music/generic music file.mp3:java.io.FileNotFoundException:无内容提供者:/storage/emulated/0/Music/generic music file.mp3

keeps popping up every time I try to play anysong.

每次我尝试播放任何歌曲时都会弹出。

The way I retrieve path to a song is:

我检索歌曲路径的方式是:

file.getAbsolutePath()

where fileis a File instance. And the way I play the song is:

其中file是一个 File 实例。我弹奏这首歌的方式是:

try {
    mediaPlayer = MediaPlayer.create(this, Uri.parse(currentTrack.getPath()));
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            nextTrack();
        }
    });
} catch (Exception ex) {
    ex.printStackTrace();
}

where thisis a reference to an instance of a class, which extends Service.

其中this是对扩展Service的类实例的引用。

Any ideas?

有任何想法吗?

采纳答案by CommonsWare

Presumably, currentTrackhas a Fileobject. If so, replace Uri.parse(currentTrack.getPath())with currentTrack.getUri(), where you implement getUri()to return the value of Uri.fromFile()for the File.

想必,currentTrackFile对象。如果是这样,更换Uri.parse(currentTrack.getPath())currentTrack.getUri(),在那里你实现getUri()返回的值Uri.fromFile()File

This solves your immediate problem, which is that you have created an invalid Uri, as it has no scheme. It also sets you up to deal with Uritypes that are not files (e.g., contentUrivalues) that you may wind up needing in the future.

这解决了您的直接问题,即您创建了一个 invalid Uri,因为它没有方案。它还使您能够处理将来可能Uri需要的非文件类型(例如,contentUri值)。

回答by Prashant Kumar Sharma

This issue occurs in Nougat or higher version if you are using nougat or higher version you have to use Content Provider.

如果您使用的是牛轧糖或更高版本的牛轧糖或更高版本,则必须使用Content Provider.

Create an XML file (for example file_provider_paths.xml) in XML resources folder:

在 XML 资源文件夹中创建一个 XML 文件(例如 file_provider_paths.xml):

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="shared" path="shared/"/>
</paths>

Now define a provider in your ApplicationManifest.xml, add this provider inside application node:

现在在 ApplicationManifest.xml 中定义一个提供者,在应用程序节点中添加这个提供者:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="<your provider authority>" //com.domainname.appname.fileprovider
    android:exported="false"
    android:grantUriPermissions="true">
  <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/file_provider_paths"/>
</provider>

Now get the shared file URI, and use it in your application where you need it.

现在获取共享文件 URI,并在您需要它的应用程序中使用它。

Uri sharedFileUri = FileProvider.getUriForFile(this, <your provider auhtority>, sharedFile);