简体   繁体   English

Android videoview-内部存储URI错误

[英]Android videoview - Error for internal storage URI

I am a beginner at Android programming and I had a doubt to be clarified. 我是Android编程的初学者,我有一个疑问需要澄清。
I tried out a tutorial on VideoView in Android and observed that, 我在Android的VideoView上试用了一个教程,并观察到,

When the specified URI string is " http://www.androidbegin.com/tutorial/AndroidCommercial.3gp ", the program works. 当指定的URI字符串为“ http://www.androidbegin.com/tutorial/AndroidCommercial.3gp ”时,该程序将运行。

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 . 我尝试将URI字符串替换为手机内部存储器(/storage/emulated/0/Movies/test.mp4)中存在的视频的位置,并且程序产生错误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. 因为两个URI字符串都指定要播放的视频。

(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 : 在videoViewActivity中,在初始化videoView之后,在onCreate方法中键入以下代码:

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

It's probably because of your Uri. 可能是因为您的Uri。 When you use Uri.parse("/blabla") it's not validating that path, is it really exist or not. 当您使用Uri.parse("/blabla")它并不验证该路径,是否确实存在。 And in your case you need to give something like "file:///storage/emulated/0/Movies/test.mp4". 在您的情况下,您需要提供类似“ file:///storage/emulated/0/Movies/test.mp4”的文件。 Or your app don't have file permission, you need to add a permission check first. 或者您的应用没有文件权限,则需要先添加权限检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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