Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot open play this video in VideoView from sd card

Hi i am trying to play video from phone's internal storage in VideoView but my phone is prompting "cannot play this video". I am getting video path in main activity passing it to projector activity.

This is the path: '/storage/emulated/0/storage/emulated/0/snaptube/download/SnapTube Video/Justin Bieber - Sorry (PURPOSE _ The Movement).mp4'

OnCreate Code:

 @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();


    }

This is how i get video path from 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);
        }
    }

Here is 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)
like image 513
Tabish Avatar asked Oct 17 '22 06:10

Tabish


1 Answers

SOLVED: Step1: Use Uri path=Uri.fromFile(new File(vidpath)); Step2: Check for the storage permission for your app in application manager of your phone

like image 158
Tabish Avatar answered Oct 21 '22 08:10

Tabish