从网络服务器下载文件到android外部存储

阿南特·索菲亚

在一个Android应用中,我试图将文件从Web服务器下载到外部存储的/ Download文件夹中。下载代码HandlerThread在服务中执行

该服务除了下载文件外还执行其他功能。下载代码如下:

public void downloadFile(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    URL url = new URL("http://192.168.1.105/download/apkFile.apk");
                    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                    InputStream inputStream = connection.getInputStream();

                    File file = new File(Environment.getExternalStorageDirectory().getPath()+"/Download/apkFile.apk");
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    int bytesRead;
                    byte[] buffer = new byte[4096];
                    while((bytesRead = inputStream.read(buffer)) != -1){
                        fileOutputStream.write( buffer, 0, bytesRead);
                    }
                    fileOutputStream.close();
                    inputStream.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

执行没有错误,但文件未下载。请提出建议。

瓦吉·查马克

您可以使用AndroidDownloadManager

此类处理文件下载的所有步骤,并为您提供有关进度扩展的信息。

您应该避免使用线程。

这是您的用法:

public void StartNewDownload(url) {       
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); /*init a request*/
    request.setDescription("My description"); //this description apears inthe android notification 
    request.setTitle("My Title");//this description apears inthe android notification 
    request.setDestinationInExternalFilesDir(context,
            "directory",
            "fileName"); //set destination
    //OR
    request.setDestinationInExternalFilesDir(context, "PATH");
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    final long downloadId = manager.enqueue(request); //start the download and return the id of the download. this id can be used to get info about the file (the size, the download progress ...) you can also stop the download by using this id     
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从“自身”从网络服务器下载文件时出现404错误

来自分类Dev

剧本中的get_url错误,正在从网络服务器下载文件

来自分类Dev

创建到网络服务器后,是否可以自动上传文件?

来自分类Dev

将android应用与网络服务器连接

来自分类Dev

双向网络服务器

来自分类Dev

用于在Android上上传文件的网络服务器

来自分类Dev

无法从自己的ipv6子网访问网络服务器,但从外部访问

来自分类Dev

在网络服务器上存储多个大小的图像或调整当前图像的大小

来自分类Dev

如何在网络服务器上存储和组织上传的图像?

来自分类Dev

网络服务器的out.flush()问题

来自分类Dev

与网络服务器相关的autoIndex是什么?

来自分类Dev

设置安全的网络服务器

来自分类Dev

设置动态网络服务器

来自分类Dev

保持我的生产网络服务器更新

来自分类Dev

网络服务器上的Memcached

来自分类Dev

在Yaws网络服务器上上传进度

来自分类Dev

简单的网络服务器无法正常工作

来自分类Dev

元素在网络服务器上消失

来自分类Dev

网络服务器显示错误的布局

来自分类Dev

网站/网络服务器容错-最佳做法

来自分类Dev

oAuth2 的 Java 网络服务器

来自分类Dev

网络服务器 php 和 mysql 滞后

来自分类Dev

自动启动网络服务器和程序

来自分类Dev

Apache 网络服务器如何检测 CNAME

来自分类Dev

使用ajax从网络服务器获取响应

来自分类Dev

无需网络服务器即可使用Javascript加载本地文件

来自分类Dev

转到网络服务器-不要使用时间戳缓存文件

来自分类Dev

如何创建目录,以便同时拥有新文件?(网络服务器)

来自分类Dev

网站文件被缓存在运行PHP 5.5.28的Debian网络服务器上

Related 相关文章

热门标签

归档