【发布时间】: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