为什么?使用意图我无法播放下载的MP4视频文件 [英] Why? I can't play downloaded mp4 video file using Intent

查看:374
本文介绍了为什么?使用意图我无法播放下载的MP4视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发一个应用程序中,我下载从URL视频文件
下载后,我打它通过一个意图。

但我得到相同的消息,每次:你无法播放此视频

下载code:

 保护字符串doInBackground(字符串... PARAMS){    档案文件=新的文件(getFilesDir(),generateFileName(PARAMS [0]));
        尝试{
            URL U =新的URL(PARAMS [0]);
            康涅狄格州的URLConnection = u.openConnection();
            INT CONTENTLENGTH = conn.getContentLength();            DataInputStream以流=新DataInputStream所(u.openStream());            字节[]缓冲区=新的字节[CONTENTLENGTH]
            stream.readFully(缓冲液);
            stream.close();            DataOutputStream类FOS =新DataOutputStream类(新的FileOutputStream(文件));
            fos.write(缓冲液);
            fos.flush();
            fos.close();        }赶上(FileNotFoundException异常五){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }        返回null;
 }

播放视频的方法:

 公共无效的playVideo(字符串文件名){       乌里数据= Uri.parse(新文件(文件名).getAbsolutePath());
       Log.i(DATA:,+数据);       意向意图=新的Intent();
       intent.setAction(android.content.Intent.ACTION_VIEW);
       intent.setDataAndType(数据,视频/ MP4);
       startActivity(意向);
}


解决方案

这code到意向提供额外的信息来源时为我工作。这不是视频数据,但图像数据,但我相信这个概念应该是相同的(我可能是错的,因为我没有尝试这个视频)。

 文件文件=新的文件(getExternalFilesDir(空),image.png);
字符串uriPath =文件://+ file.getPath();
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(uriPath));

请注意,我提供给意向图像这门课程之前下载,然后保存在应用程序的目录(这个我getExternalFilesDir得到(空))。然后将图像传送到一个​​意图作为流。

如果这没有帮助,那么你或许检查是否安装了可以处理您的视频类型的任何应用程序(它是MP4或者是什么格式?)

I have to develop an application in which I am downloading a video file from an URL After downloading, I am playing it through an Intent.

But every time I am getting the same message: "You can't play this video."

download code:

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

method for playing the video:

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

解决方案

This code worked for me when supplying extra infomation to an intent. It's not video data, but image data, but I believe the concept should be the same (I could be wrong, since I didn't try this on video).

File file = new File(getExternalFilesDir(null), "image.png");
String uriPath = "file://"+file.getPath(); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(uriPath));

Note that the image I am supplying to the intent is downloaded before this of course and then saved in the directory of the app (this I get with getExternalFilesDir(null)). Then the image is passed to an intent as a stream.

If this does not help you then maybe check if there are any apps installed that can handle your type of video (is it mp4 or what is the format?)

这篇关于为什么?使用意图我无法播放下载的MP4视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆