【问题标题】:Android: Download Manager Request to Cache directoryAndroid:下载管理器请求缓存目录
【发布时间】:2019-06-04 15:12:44
【问题描述】:

我需要使用本机下载管理器将敏感文件保存到缓存目录。用户无法发现的位置。我可以使用DownloadManager.Request() 轻松下载到用户的外部文件系统。虽然使用setDestinationInExternalPublicDir() 或任何其他方式来设置目标,但不允许我保存到缓存目录。我在这里遗漏了什么吗?

【问题讨论】:

    标签: android caching


    【解决方案1】:

    DownloadManager 中的构建无法将文件保存到内部目录。仅限外部目录,如 SD 卡和其他公共目录,例如视频或照片文件夹。

    【讨论】:

      【解决方案2】:

      您必须忘记下载管理器中的 Android 版本并创建自己的管理器。然后就可以下载到路径:getCacheDir().getAbsolutePath();

      这是一个无需内置管理器自行下载文件的示例代码

      public String downloadFile(String fileURL, String fileName) {
      
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy);
      
              Log.d(TAG, "Downloading...");
              try {
                  int lastDotPosition = fileName.lastIndexOf('/');
                  if( lastDotPosition > 0 ) {
                      String folder = fileName.substring(0, lastDotPosition);
                      File fDir = new File(folder);
                      fDir.mkdirs();
                  }
      
                  //Log.i(TAG, "URL: " + fileURL);
                  //Log.i(TAG, "File: " + fileName);
                  URL u = new URL(fileURL);
                  HttpURLConnection c = (HttpURLConnection) u.openConnection();
                  c.setRequestMethod("GET");
                  c.setReadTimeout(30000);
                  c.connect();
                  double fileSize  = (double) c.getContentLength();
                  int counter = 0;
                  while ( (fileSize == -1) && (counter <=30)){
                      c.disconnect();
                      u = new URL(fileURL);
                      c = (HttpURLConnection) u.openConnection();
                      c.setRequestMethod("GET");
                      c.setReadTimeout(30000);
                      c.connect();
                      fileSize  = (double) c.getContentLength();
                      counter++;
                  }
      
                  File fOutput = new File(fileName);
                  if (fOutput.exists())
                      fOutput.delete();
      
                  BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(fOutput));
                  InputStream in = c.getInputStream();
                  byte[] buffer = new byte[8192];
                  int len1 = 0;
                  int downloadedData = 0;
                  while ((len1 = in.read(buffer)) > 0) {
                      downloadedData += len1;
                      f.write(buffer, 0, len1);
                  }
                  Log.d(TAG, "Finished");
                  f.close();
      
                  return fileName;
              }
              catch (Exception e) {
                  e.printStackTrace();
                  Log.e(TAG, e.toString());
                  return null;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-13
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 2012-05-20
        相关资源
        最近更新 更多