Android: Video is playable from gallery but when I

2019-05-11 21:16发布

Here is my original question

I've implemented the answer there but still the problem persist.

Here is the gist: 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 the gallery, 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

The file exist from the answer that I got. But still the problem persist, and I have no idea what went wrong.

Any insight is appreciated. Thanks

Edit:To those who didn't see the answer in the first question. I've already implemented this:

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

The file exist since I've checked it. I'm wondering if there is a problem with the file naming convention.

Also I'm debugging from my device, Samsung Galaxy Ace, Android 2.3.6, compiling with 4.2 sdk.

Edit 2: I've tried renaming the video into simpler one and now the video works, my guess is that the the file has a filename length limitation or an naming convention.

3条回答
趁早两清
2楼-- · 2019-05-11 21:22

You need to use the file scheme:

So you should use intent.setDataAndType("file://" + uri, "video/*");

查看更多
走好不送
3楼-- · 2019-05-11 21:33

I've tried different solution. But I guess the problem occurred because of naming convention. I have tried renaming the file into a simpler format and it works. Also the problem with my original file name is that I have a "--" in the file name.

查看更多
家丑人穷心不美
4楼-- · 2019-05-11 21:44

This code is from a working app that I made, try it out.

Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(
                    Uri.parse("file://"+ file.getAbsolutePath()),
                    "video/*");
查看更多
登录 后发表回答