android - 用安卓下载文件

标签 android popup download

我正在尝试将文件从 Web 保存到我的 Android 设备,但我不知道该怎么做。 URL 地址会打开一个空白页面和一个弹出下载框,其中包含我要下载的文件,以便我可以选择保存文件的位置。

我的问题是我不知道如何管理该框才能下载文件。我不想下载该页面上显示的内容,因为该页面是空白的。所以我不能使用这段代码:

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

如果有人能指出我从哪里开始寻找或给我一个解决方案,我将不胜感激。

最佳答案

我解决了!

           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包围了。并记得在地址前插入协议(protocol),否则将无法正常工作! 希望对您有所帮助!

关于android - 用安卓下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8645166/

相关文章:

iOS 在应用程序启动时下载 SQLite 数据库

java - Java 中的高效 Zip 文件流

android - RecyclerView 适配器 onCreateViewHolder 与 onBindViewHolder 未调用 : view empty

android - 防止在 ImageView 单击时显示状态栏

android - 如何从不受支持的国家(拉脱维亚)向 Android 市场添加非免费应用程序?

javascript - 在页面顶部显示颜色框

ios - 将音频文件下载到文件管理器中需要从本地路径播放它

android - 项目单击时的 RecyclerView 动画

java - 右键单击多项选择和 JPopupMenu

javascript - 检查文本框中是否输入了文本以及是否选择了单选按钮+警报并直接进入页面