Get java.io.FileNotFoundException when using VideoView
I have the following code:
VideoView videoView = (VideoView)findViewById(R.id.instructionsvideo);
assert videoView != null;
videoView.setVideoPath("android.resource://" + getPackageName() + R.raw.testnatureclip);
videoView.start();
“testnatureclip” is located in the original folder:
For some reason, the file turns red after I build the project.
Here’s the error I get:
com.roymunson.vroy.copypastakeyboard W/MediaPlayer: Unable to open files on the client side; Try server-side: java.io.FileNotFoundException: Authorization package not found: android.resource://com.roymunson.vroy.copypastakeyboard2131165184
mp4 should be H.264 encoded, I don’t know if the online encoding service used is useful.
Also, if important, the size of the videoview is different from the size of the file.
What is the problem? Is the file path incorrect, or am I missing some elements when initializing the video view?
Update one:
Using User8’s solution I get the following error:
roymunson.vroy.copypastakeyboard W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: /2131165184
10-01 17:36:20.912 28156-28156/com.roymunson.vroy.copypastakeyboard W/VideoView: Unable to open content: /2131165184
java.io.IOException: setDataSource failed.
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1100)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1074)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1028)
at android.widget.VideoView.openVideo(VideoView.java:346)
at android.widget.VideoView.-wrap0(VideoView.java)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:623)
at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Solution
I found 2 solutions :
String uriPath = "android.resource://" + getPackageName() + "/raw/testnatureclip";
Uri uri = Uri.parse(uriPath);
videoView.setVideoURI(uri);
or
videoView.setVideoURI(Uri.parse("android.resource://ABSOLUTE_PACKAGE_NAME/" + R.raw.testnatureclip));