【问题标题】:Download file from a webserver into android external storage将文件从网络服务器下载到 android 外部存储
【发布时间】:2015-05-07 16:08:55
【问题描述】:

在一个 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();
    }

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

【问题讨论】:

  • 那么文件存放在哪个目录下?
  • showOutput 现在告诉你什么?在 catch 块中也放一个 showOutput。有联系吗?将日志语句放入您的代码中,以便您可以按照流程进行操作。
  • @greenapps 你说得对,这并不意味着什么......我已经编辑了代码。文件无法下载的任何建议..
  • 男人不要删除它,而是用它来检查发生了什么。现在它显示了网址吗?因为如果它执行了所有代码。所以把它放回去。还有一个在 catch 块中。然后添加很多 Log.d() 语句。让我们看看您如何更新代码并告诉我们发生了什么。
  • @PhanDinhThai 该文件将存储在 /Download 目录中..

标签: android downloading-website-files


【解决方案1】:

您可以使用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     
}

【讨论】:

  • 我怎样才能知道下载何时完成,因为我需要对下载的文件启动一些功能。
  • DownloadManager.Query q = new DownloadManager.Query(); q.setFilterById(mDownloadId); Cursor cursor = manager.query(q); cursor.moveToFirst(); 这将从查询中初始化光标。 int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); => 这会告诉你文件的总大小int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); => 这会告诉你你从这个文件下载了多少
  • @WajdiChamakhi 如果您需要一次下载多个文件怎么办?
【解决方案2】:

打电话

connection.setDoInput(true);
connection.connect();

之前

InputStream inputStream = connection.getInputStream();

【讨论】:

  • 下载仍然没有开始!
猜你喜欢
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多