Android简易媒体播放器开发报错求助:VideoView无法播放视频
Hey there, let's break down why you're hitting this playback error. Looking at your logcat output, the critical line that tells us what's wrong is:
java.io.FileNotFoundException: No content provider: /document/primary:WhatsApp/Media/WhatsApp Video/VID-20191031-WA0041.mp4
What's causing the issue?
When you use Intent.ACTION_GET_CONTENT to pick a file, the Uri returned by data.getData() isn't a direct file system path—it's a Content Provider URI (typically starting with content://). Calling getPath() on this Uri only gives you a fragment of that URI, not the actual location of the file on the device. When you try to parse that string back into a Uri with Uri.parse(PathHolder), the system can't find a valid content provider to access the file, hence the error.
The Simple Fix
You don't need to convert the Uri to a string path at all. Just use the original Uri directly with VideoView.setVideoURI(). Here's your corrected onActivityResult method:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 7: if (resultCode == RESULT_OK) { // Use the original Uri directly, no need for getPath() Uri videoUri = data.getData(); Toast.makeText(this, videoUri.toString(), Toast.LENGTH_SHORT).show(); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); videoPlayer.setVideoURI(videoUri); videoPlayer.requestFocus(); videoPlayer.start(); } break; } }
Extra Debugging Tip
To make it easier to diagnose future playback issues, add an error listener to your VideoView—this will give you specific details if something goes wrong:
videoPlayer.setOnErrorListener((mediaPlayer, what, extra) -> { Toast.makeText(this, "Playback error: Code " + what + ", Extra " + extra, Toast.LENGTH_LONG).show(); return true; });
Also, keep in mind that on Android 10 (API 29) and above, direct access to file paths is restricted—using Content Uris is the recommended (and required) way to interact with user-selected files, so this fix aligns with modern Android best practices.
Your Original Code (for reference)
PlayerActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.net.Uri; import android.os.Bundle; import android.widget.Toast; import android.widget.VideoView; public class PlayerActivity extends AppCompatActivity { VideoView videoPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoPlayer = findViewById(R.id.videoView); Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); startActivityForResult(intent, 7); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 7: if (resultCode == RESULT_OK) { String PathHolder = data.getData().getPath(); Toast.makeText(this, PathHolder, Toast.LENGTH_SHORT).show(); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); videoPlayer.setVideoURI(Uri.parse(PathHolder)); videoPlayer.requestFocus(); videoPlayer.start(); } } } }
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="318dp" tools:layout_editor_absoluteY="716dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
Logcat Error
2020-08-09 12:55:37.585 21894-21894/com.phantom.player W/MediaPlayer: Couldn't open /document/primary:WhatsApp/Media/WhatsApp Video/VID-20191031-WA0041.mp4 java.io.FileNotFoundException: No content provider: /document/primary:WhatsApp/Media/WhatsApp Video/VID-20191031-WA0041.mp4 at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1680) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1510) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1427) at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1149) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1121) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1145) at android.widget.VideoView.openVideo(VideoView.java:412) at android.widget.VideoView.access$2200(VideoView.java:83) at android.widget.VideoView$7.surfaceCreated(VideoView.java:694) at android.view.SurfaceView.updateSurface(SurfaceView.java:926) at android.view.SurfaceView.windowStopped(SurfaceView.java:315) at android.view.ViewRootImpl.setWindowStopped(ViewRootImpl.java:1973) at android.view.WindowManagerGlobal.setStoppedState(WindowManagerGlobal.java:709) at android.app.Activity.performRestart(Activity.java:8039) at android.app.ActivityThread.handleSleeping(ActivityThread.java:5007) at android.app.ActivityThread.access$2500(ActivityThread.java:268) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2080) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7807) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
内容的提问来源于stack exchange,提问作者ADP

