简体   繁体   中英

Android videoview - Error for internal storage URI

I am a beginner at Android programming and I had a doubt to be clarified.
I tried out a tutorial on VideoView in Android and observed that,

When the specified URI string is " http://www.androidbegin.com/tutorial/AndroidCommercial.3gp ", the program works.

I tried replacing the URI string with the location of a video present in the phone's internal storage (/storage/emulated/0/Movies/test.mp4) and the program produced the error java.io.IOException: setDataSource failed .

My question is what does the error signify and why does it occur ? since both the URI string's do specify the video to be played.

(Note: I followed this tutorial )

Try this code to get a video from gallery:

// in onCreate method    
Intent getVid= new Intent();
                            getVid.setType("video/*");
                            getVid.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(getVid, "Select a video" ),


@Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                } if (requestCode == 1 && resultCode == RESULT_OK){
                    String videoUrl = data.getData().toString();
                    Intent i = new Intent(MainActivity.this , videoViewActivity.class);
                    i.putExtra("vid" , videoUrl);
                    startActivity(i);

                }

in videoViewActivity type this code in onCreate method after initializing videoView :

String path = getIntent().getStringExtra("vid");
videoView.setVideoPath(path);
videoView.start();

It's probably because of your Uri. When you use Uri.parse("/blabla") it's not validating that path, is it really exist or not. And in your case you need to give something like "file:///storage/emulated/0/Movies/test.mp4". Or your app don't have file permission, you need to add a permission check first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM