Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 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 raw folder:

enter image description here

For some reason after I build the project the file turns red.

Here is the error I get: com.roymunson.vroy.copypastakeyboard W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No package found for authority: android.resource://com.roymunson.vroy.copypastakeyboard2131165184

The mp4 should be encoded in H.264 format, but I don't know if the online encoding service I used worked.

Additionally, the videoview does not have the same dimensions as the file, if that matters.

What is the problem? Is the file path incorrect, or I am I missing some element in initializing the videoview?

UPDATE ONE:

Using User8's solution I got 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)
like image 778
Roymunson Avatar asked Oct 01 '16 21:10

Roymunson


2 Answers

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));
like image 177
Roymunson Avatar answered Sep 20 '22 19:09

Roymunson


Try this one out, specifying an absolute package name path and using Uri:

 videoView.setVideoURI(Uri.parse("android.resource://com.roymunson.vroy.copypastakeyboard/" 
                                  + R.raw.testnatureclip));

Also, why don't you just use getPackageResourcePath() directly to access resources?

Then, you generally can't access them that way, you should use only their resourceID, here is a discussion on exactly that topic: Access resource files in Android.

like image 28
Dimitar Avatar answered Sep 21 '22 19:09

Dimitar