Search code examples
androidandroid-intentandroid-video-player

Intent.ACTION_VIEW video: external storage video "Sorry, this video cannot be played", but in regular android player, it is playable


So I'm playing a video from external storage(sdcard), I'm having a problem with playing the video and this is my code:

Uri uri = Uri.parse(url);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");

It prompts "Sorry, this video cannot be played", but in regular android player, it is playable. I printed the url and this is what I got:

VideoPlayer url: file:///mnt/sdcard/foldername/video-2012-12-26-21-26--44.mp4

Is it a problem with the uri? If no, can you point me to the right direction. Also if I try a foldername that have a dot(.) will I have a problem?

url = file:///mnt/sdcard/Android/data/com.example.project/video-2012-12-26-21-26--44.mp4

Thanks.

Edit: Since my real problem was not solve, and nobody want to answer anymore, I open up a new question: Android: Video is playable from gallery but when I play it using Intent.ACTION_VIEW type video, cannot play

Partial Answer to my problem:

The problem occurs because of naming convention. I think the player doesn't accept file names that have "--" in it. So I solved this problem by changing the file name format.

//Disclaimer: I don't have a full explanation and source but this info is just deduced from my workaround.


Solution

  • make sure you are passing right sdcard path with Intent. best way is use Environment.getExternalStorageDirectory() for getting SDCARD path instead of passing static string :

    intent = new Intent(Intent.ACTION_VIEW);
    
    File sdCard = Environment.getExternalStorageDirectory();
    File file = new File(sdCard, "/foldername/video-2012-12-26-21-26-44.mp4");
    
    intent.setDataAndType(Uri.fromFile(file), "video/*");
    
    startActivity(intent);