Android 为什么?我能';t使用Intent播放下载的mp4视频文件

Android 为什么?我能';t使用Intent播放下载的mp4视频文件,android,android-video-player,video-player,Android,Android Video Player,Video Player,我必须开发一个应用程序,其中我从一个URL下载一个视频文件 下载后,我通过意图播放它 但每次我都得到同样的信息:“你不能播放这个视频。” 下载代码: protected String doInBackground(String... params) { File file = new File(getFilesDir(), generateFileName(params[0])); try { URL u = new URL(params[0])

我必须开发一个应用程序,其中我从一个URL下载一个视频文件 下载后,我通过意图播放它

但每次我都得到同样的信息:“你不能播放这个视频。”

下载代码:

protected String doInBackground(String... params) {

    File file = new File(getFilesDir(), generateFileName(params[0]));
        try {
            URL u = new URL(params[0]);
            URLConnection conn = u.openConnection();
            int contentLength = conn.getContentLength();

            DataInputStream stream = new DataInputStream(u.openStream());

            byte[] buffer = new byte[contentLength];
            stream.readFully(buffer);
            stream.close();

            DataOutputStream fos = new DataOutputStream(new FileOutputStream(file));
            fos.write(buffer);
            fos.flush();
            fos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
 }
播放视频的方法:

public void playVideo(String fileName) {

       Uri data = Uri.parse(new File(fileName).getAbsolutePath());
       Log.i("DATA: ",""+data);

       Intent intent = new Intent();
       intent.setAction(android.content.Intent.ACTION_VIEW);
       intent.setDataAndType(data, "video/mp4");
       startActivity(intent); 
}

当我向意图提供额外信息时,这段代码对我很有用。这不是视频数据,而是图像数据,但我相信这个概念应该是一样的(我可能错了,因为我没有在视频上尝试这个)

请注意,我提供给intent的图像当然是在此之前下载的,然后保存在应用程序的目录中(我使用getExternalFilesDir(null)获得)。然后将图像作为流传递给intent


如果这对您没有帮助,那么可能需要检查是否安装了可以处理您的视频类型的应用程序(是mp4还是什么格式?

尝试通过浏览器下载并播放此视频。如果不起作用,则视图似乎没有compability格式。如果它能工作,发布你的readFully方法你的文件路径正确吗?你成功了吗?你有哪个安卓版本?我也有类似的问题。在Android 4.2(三星Galaxy S3)上运行良好,但在4.4.4(Nexus 4)上运行不好
File file = new File(getExternalFilesDir(null), "image.png");
String uriPath = "file://"+file.getPath(); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(uriPath));