我怎么能保存的Andr​​oid视频是在服务器端? [英] How I can save a video in Android which is on server side?

查看:245
本文介绍了我怎么能保存的Andr​​oid视频是在服务器端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自服务器的视频的网址的用的 JSON 的的帮助。

I am getting a video URL from server with the help of JSON.

我怎样才能将其保存在我的Andr​​oid项目? (我想通过电子邮件发送此视频也。)

How can I save it in my android project? (I want to send this Video through Email also.)

推荐答案

试试这个,

public String downloadImages(String serverUrl,String directory)
    {

        if (serverUrl == null)
            return null;

        String LocalFilePath=serverUrl.substring(serverUrl.lastIndexOf("/")+1, serverUrl.length());
        File file =  new File(directory, LocalFilePath);
         if (!file.exists())
         {
                int bytesread = 0;
                byte[] bytes = new byte[1024];  
                InputStream strm = null;
                HttpResponse response = null;
                HttpEntity entity = null;
                String LocalFile = null;
                FileOutputStream foStream;
                BufferedOutputStream writer = null;
                try {
                    DefaultHttpClient httpclient = new DefaultHttpClient();
                    serverUrl = serverUrl.replace(" ", "%20");
                    HttpGet httpget = new HttpGet(serverUrl);   

                    response = httpclient.execute(httpget);

                    if (response == null) 
                    {
                        return null;
                    }
                    entity = response.getEntity();
                    strm = entity.getContent();         
                    File dire = new File(directory);
                    if (!dire.isDirectory()) 
                    {
                        dire.mkdirs();
                    }
                    LocalFile = directory+LocalFilePath;
                    directory = null;
                    foStream = new FileOutputStream(LocalFile);
                    writer = new BufferedOutputStream(foStream);            

                    do 
                    {
                        bytesread = strm.read(bytes, 0, bytes.length);
                        if(bytesread > 0)
                        {   
                            writer.write(bytes, 0, bytesread);
                            writer.flush();
                        }
                    } while (bytesread > 0);                    

                    writer.close();
                    foStream.close();

                    strm.close();               
                    return LocalFile;
                }
                catch(Exception e)  
                {   
                    file.delete();
                    e.printStackTrace();
                    return null;
                }   
         }
         else
         {
             return "file exist";
         }
    }

这将从服务器上下载文件并保存在您的特定文件路径

This will download file from server and save at your given filepath

这篇关于我怎么能保存的Andr​​oid视频是在服务器端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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