【问题标题】:Android how to create a new file for each download?Android如何为每次下载创建一个新文件?
【发布时间】:2018-01-30 05:26:58
【问题描述】:

我正在创建一个应用程序,每次按下下载按钮都会在我的存储中创建一个 pdf 文件。我的问题是新创建的文件会覆盖具有相同文件名的现有文件。我需要使用新文件名下载。我是什么尝试

      dirpath = 
      android.os.Environment.getExternalStorageDirectory().toString();
      // int increase=0;
       file = new File(dirpath+"/NewPDF.pdf");
       if(file.exists()){
           increase++;
           file = new File(dirpath + "/NewPDF" +increase+".pdf");}

上面这几行程序创建文件,但我需要打开最后一个下载文件

      Intent intent = new Intent(Intent.ACTION_VIEW);
      Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf"));
      intent.setDataAndType(uri, "application/pdf");
      intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
      startActivity(intent);

以上这些代码行会产生错误。

      Error:File no longer exist,the file maybe deleted or changed or 
     removed by another program

1.创建一个新文件并在我单击按钮时防止覆盖 2.打开上次下载的文件 我是 Android 新手,对此有点困惑,需要你们的帮助。

【问题讨论】:

  • 使用System.currentTimeMillis(); 而不是increase
  • 好的,为什么要下载已经下载的文件?我的意思是,不应该是这样的。
  • @Wizard 每次我更改内容并设置下载..所以已经下载文件将受到限制。我的问题是所有文件都是用相同的文件名创建的,所以它会覆盖现有文件,现在我想要为每次下载动态设置文件名。
  • @Karthi028 但是,您为什么不直接删除以前的文件并下载最新的文件。每次下载新的而不删除前一个 - 是浪费存储空间。我的方法 - 你的服务器文件将有一些唯一的 id,用那个 id 保存你的文件。通过这样做..即使它在本地存储中也很容易跟踪。

标签: android android-intent android-file android-external-storage android-fileprovider


【解决方案1】:

您调用Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf")); 来打开文件,所以为什么要添加 /NewPDF.pdf,因为它已经在您的文件中。删除它。

替换这一行

Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf"));

Uri uri = Uri.fromFile(new File( file));

【讨论】:

    【解决方案2】:

    您正在为您创建的每个文件分配相同的名称。尝试给一个像这样的唯一名称:

    UUID uuid = UUID.randomUUID();
    String randomUUIDString = uuid.toString();
    

    randomUUIDString 将是 pdf 文件的唯一字符串。此外,如果您想使用这些字符串,请将它们存储在 SQLite 数据库中。

    【讨论】:

    • ..是的,当然! :)
    【解决方案3】:

    您在意图中输入的路径与创建的文件路径不匹配。

    试试这个。

    dirpath = android.os.Environment.getExternalStorageDirectory().toString();
    String lastFilePath = null;
          // int increase=0;
           file = new File(dirpath+"/NewPDF.pdf");
           if(file.exists()){
               increase++;
               file = new File(dirpath + "/NewPDF" +increase+".pdf");}
               lastFilePath =  file.getAbsolutePath();
    

    然后就可以这样打开文件了。

    Intent intent = new Intent(Intent.ACTION_VIEW);
          Uri uri = Uri.fromFile(new File(lastFilePath));
          intent.setDataAndType(uri, "application/pdf");
          intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
          startActivity(intent);
    

    希望对你有帮助:)

    【讨论】:

      【解决方案4】:

      if 替换为while 并显示Intent

      dirpath = android.os.Environment.getExternalStorageDirectory().toString();
      int increase = 0;
      file = new File(dirpath+"/NewPDF.pdf");
      while(file.exists()) {
          increase++;
          file = new File(dirpath + "/NewPDF" +increase+".pdf");
      }
      file.createNewFile();
      
      ... store data in file here ...
      
      Intent intent = new Intent(Intent.ACTION_VIEW);
      Uri uri = Uri.fromFile(file);
      intent.setDataAndType(uri, "application/pdf");
      intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
      startActivity(intent);
      

      【讨论】:

        【解决方案5】:

        使用时间戳代替计数器,这样您每次都可以创建一个新文件。

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
            Date timeStamp = new Date();   
        
             dirpath = 
                  android.os.Environment.getExternalStorageDirectory().toString();
                  // int increase=0;
                   file = new File(dirpath+"/NewPDF.pdf");
                   if(file.exists()){
                       increase++;
                       file = new File(dirpath + "/NewPDF" +formatter.format(timeStamp )+".pdf");}
        

        【讨论】:

          猜你喜欢
          • 2017-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多