如何将下载的文件保存在应用程序内存的缓存文件夹中?
How to save downloaded files in the cache folder of the app's memory?
我知道之前有人问过类似的问题,但确实没有有效的解决方案。我正在通过 DownloadManager 下载多个 PDF 文件,我想将这些文件保存到应用程序内存的缓存文件夹中,但找不到任何方法。
这是我的代码:
public class MainActivity extends AppCompatActivity {
Button btn;
DownloadManager downloadManager;
List<String> myList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList.add("https://doc.lagout.org/programmation/Actionscript%20-%20Flash%20-%20Flex%20-%20Air/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29.pdf");
myList.add("http://www.csc.kth.se/utbildning/kth/kurser/DD143X/dkand11/Group1Mads/andreas.ulvesand.daniel.eriksson.report.pdf");
myList.add("http://enos.itcollege.ee/~jpoial/allalaadimised/reading/Android-Programming-Cookbook.pdf");
myList.add("https://x.coe.phuket.psu.ac.th/warodom/242-320/ebook/9781785883262-ANDROID_PROGRAMMING_FOR_BEGINNERS.pdf");
myList.add("https://www.cs.cmu.edu/~bam/uicourse/830spring09/BFeiginMobileApplicationDevelopment.pdf");
myList.add("https://commonsware.com/Android/Android-1_0-CC.pdf");
btn = findViewById(R.id.download_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
for(int i = 0; i < myList.size(); i++) {
Uri uri = Uri.parse(myList.get(i));
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadManager.enqueue(request);
}
}
});
}
}
您可以使用设置下载文件的本地目标。
request.setDestinationUri(Uri uri)
您需要使用context.getCacheDir()
获取缓存目录并将您的文件保存到缓存。
最后,在您的路径中 xml 添加以下代码以允许提供商获取 uri。
<cache-path name="cache_files" path="." />
Internal storage:
It's always available. Files saved here are accessible by only your
app. When the user uninstalls your app, the system removes all your
app's files from internal storage.
Android 内置下载管理器无法访问您的应用程序内部存储,因此您只能通过调用 setDestinationInExternalFilesDir
或 setDestinationInExternalPublicDir
将文件保存到外部存储目录中.
但如果你想将它们保存在你的内部存储中,只需注册一个接收,然后将文件从外部存储复制到内部存储。
我知道之前有人问过类似的问题,但确实没有有效的解决方案。我正在通过 DownloadManager 下载多个 PDF 文件,我想将这些文件保存到应用程序内存的缓存文件夹中,但找不到任何方法。
这是我的代码:
public class MainActivity extends AppCompatActivity {
Button btn;
DownloadManager downloadManager;
List<String> myList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList.add("https://doc.lagout.org/programmation/Actionscript%20-%20Flash%20-%20Flex%20-%20Air/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29.pdf");
myList.add("http://www.csc.kth.se/utbildning/kth/kurser/DD143X/dkand11/Group1Mads/andreas.ulvesand.daniel.eriksson.report.pdf");
myList.add("http://enos.itcollege.ee/~jpoial/allalaadimised/reading/Android-Programming-Cookbook.pdf");
myList.add("https://x.coe.phuket.psu.ac.th/warodom/242-320/ebook/9781785883262-ANDROID_PROGRAMMING_FOR_BEGINNERS.pdf");
myList.add("https://www.cs.cmu.edu/~bam/uicourse/830spring09/BFeiginMobileApplicationDevelopment.pdf");
myList.add("https://commonsware.com/Android/Android-1_0-CC.pdf");
btn = findViewById(R.id.download_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
for(int i = 0; i < myList.size(); i++) {
Uri uri = Uri.parse(myList.get(i));
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadManager.enqueue(request);
}
}
});
}
}
您可以使用设置下载文件的本地目标。
request.setDestinationUri(Uri uri)
您需要使用context.getCacheDir()
获取缓存目录并将您的文件保存到缓存。
最后,在您的路径中 xml 添加以下代码以允许提供商获取 uri。
<cache-path name="cache_files" path="." />
Internal storage: It's always available. Files saved here are accessible by only your app. When the user uninstalls your app, the system removes all your app's files from internal storage.
Android 内置下载管理器无法访问您的应用程序内部存储,因此您只能通过调用 setDestinationInExternalFilesDir
或 setDestinationInExternalPublicDir
将文件保存到外部存储目录中.
但如果你想将它们保存在你的内部存储中,只需注册一个接收,然后将文件从外部存储复制到内部存储。