无法在SDV卡中通过VideoView播放此视频

时间:2017-09-11 05:03:35

标签: mp4 android-videoview android-video-player

您好我正在尝试从VideoView中的手机内部存储中播放视频,但我的手机正在提示"无法播放此视频"。我正在主要活动中将视频路径传递给投影仪活动。

这是路径:' / storage / emulated / 0 / storage / emulated / 0 / snaptube / download / SnapTube Video / Justin Bieber - 抱歉(PURPOSE _ The Movement).mp4'

OnCreate代码:



 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_projector);

        Intent intent = getIntent();
        String vidpath = intent.getExtras().getString("path");
        p = (TextView) findViewById(R.id.videopath);
        p.setText(vidpath);

        Uri path= Uri.parse(Environment.getExternalStorageDirectory()+vidpath);

        viewvid=(VideoView) this.findViewById(R.id.videoView);
        Log.i("vidpath","here");
        viewvid.setVideoURI(path);
        viewvid.start();


    }




这就是我从MainActivity获取视频路径的方式:



 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setTitle("Video Projector");
        gall = (Button) findViewById(R.id.gallery);
        gall.setOnClickListener(this);
    }

    @ Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_VIDEO) {
                selectedVideoPath = getPath(data.getData());
                if(selectedVideoPath == null) {
                    finish();
                } else {
                    Toast.makeText(this,"A Video was selected",Toast.LENGTH_LONG).show();
                    Intent i = new Intent(this,projector.class);
                    i.putExtra("path",selectedVideoPath);
                    startActivity(i);
                }
            }
        }
        finish();
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if(cursor!=null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        else return null;
    }



    @Override
    public void onClick(View v) {
        if (v==gall)
        {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, SELECT_VIDEO);
        }
    }




这是Log:



09 - 11 09: 48: 33.341 27291 - 27291 / com.example.tabish.vidprojector W / MediaPlayer: Couldn 't open /storage/emulated/0/storage/emulated/0/snaptube/download/SnapTube Video/Justin Bieber - Sorry (PURPOSE _ The Movement).mp4: java.io.FileNotFoundException: No content provider: /storage/emulated/0/storage/emulated/0/snaptube/download/SnapTube Video/Justin Bieber - Sorry (PURPOSE _ The Movement).mp4
09 - 11 09: 48: 33.342 27291 - 27291 / com.example.tabish.vidprojector I / MediaPlayer: setDataSource(/storage/emulated / 0 / storage / emulated / 0 / snaptube / download / SnapTube Video / Justin Bieber - Sorry(PURPOSE _ The Movement).mp4)
09 - 11 09: 48: 33.342 547 - 628 / ? I / MediaPlayerService : setDataSource(/storage/emulated / 0 / storage / emulated / 0 / snaptube / download / SnapTube Video / Justin Bieber - Sorry(PURPOSE _ The Movement).mp4)
09 - 11 09: 48: 33.365 547 - 27641 / ? E / FileSource : Failed to open file '/storage/emulated/0/storage/emulated/0/snaptube/download/SnapTube Video/Justin Bieber - Sorry (PURPOSE _ The Movement).mp4'.(No such file or directory)




1 个答案:

答案 0 :(得分:1)

<强> 解决: 第1步:使用Uri path=Uri.fromFile(new File(vidpath)); 第2步:在手机的应用管理器中检查应用的存储权限