文件下载与Android [英] File download with android

查看:112
本文介绍了文件下载与Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网上的文件保存到我的Andr​​oid设备,但我不知道该怎么做。该URL地址打开一个空白页,并弹出下载框,我要下载的文件,所以我可以选择保存位置。

I am trying to save a file from the web to my Android device, but I do not know how to do it. The URL address opens a blank page and a popup download box with the file that I want to download so I can choose where to save it.

我的问题是,我不知道如何管理这个盒子,以下载文件。它不像我想下载的东西,显示该网页上,因为页面是空白。所以我不能用这个code:

My problem is that I do not know how to manage that box in order to download the file. It is not like I want to download something that is shown on that page because the page is blank. So I cannot use this code :

         Context context = thisClass.this;
         Drawable image = ImageOperations(context,
          "http://android.okhelp.cz/images/adictionary/ad_4.png"
          ,"image.jpg");
         ImageView imgView;
         imgView = (ImageView)findViewById(R.id.idImageView);
         imgView.setImageDrawable(image);

private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

我将不胜感激,如果有人可以从哪里开始寻找点我还是给了我一个解决方案。

I would be grateful if someone could point me where to start looking or gave me a solution.

推荐答案

我解决了!

           File card2= Environment.getExternalStorageDirectory();
           File dir2= new File (card2.getAbsolutePath()+"/MyFiles");
            dir2.mkdirs();
           new DefaultHttpClient().execute(new HttpGet(your_url_here-this_is_a_string)).getEntity().writeTo(
            new FileOutputStream(new File(dir2,your_file_on_sdcard-this_is_string)));

这周围也有try / catch语句。记住地址之前插入协议,否则将无法正常工作! 希望它可以帮助!

This is also surrounded by try/catch. And remember to insert the protocol before the address or it won't work! Hope it helps!

这篇关于文件下载与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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