解决MediaPlayer: Couldn't open /storage/emulated:java.io.FileNotFoundException: No content provider问题
项目中用到了MediaPlayer播放本地视频文件的功能,遇到了一个很奇葩的问题,在4.0的设备上可以完美播放,但是在7.0的设备上死活播放不了,一直报如下错误
错误信息里出现了这几个字眼“No content provider”,然后就被带沟里去了,一直往这个方向去找问题,网上也有很多说就是没有提供content provider的问题,然后就跟着实现了content provider,然并卵,折腾了一天还是无法播放
后来同事的一句话提醒了我,她说会不会是权限问题,这才想起来6.0以上设备为运行时权限,需到手机设置那边放开存储的权限,或者打开应用时主动请求用户允许权限。
后来仔细一看log还是有提示“Permission denied”这个字眼的,但就是没注意,一直执着于content provider,结果一直被带坑里去,网上也没人给出正确的答案,这里记录下,防止有人跟我一样入坑无法自拔。
开发就是这样,有时候一个小问题就让你折腾老半天,所以一定要心细,再者不行就问问被人,可能别人已入过坑,可以拉你一把。
2013-01-18 23:20:26.422 7020-7020/com.xindecoiot.rfidshelf W/MediaPlayer: Couldn't open /storage/emulated/0/video.mp4: java.io.FileNotFoundException: No content provider: /storage/emulated/0/video.mp4
2013-01-18 23:20:26.423 7020-7020/com.xindecoiot.rfidshelf W/VideoView: Unable to open content: /storage/emulated/0/video.mp4
java.io.FileNotFoundException: /storage/emulated/0/video.mp4 (Permission denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1252)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1221)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1163)
at android.widget.VideoView.openVideo(VideoView.java:346)
at android.widget.VideoView.-wrap0(VideoView.java)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:622)
at android.view.SurfaceView.updateWindow(SurfaceView.java:618)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:161)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6338)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:755)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)