我知道以前有人问过类似的问题,但确实没有可行的解决方案。我正在通过 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); } } }); }}
2 回答
胡子哥哥
TA贡献1825条经验 获得超6个赞
您可以使用设置下载文件的本地目的地。
request.setDestinationUri(Uri uri)
您需要使用context.getCacheDir()
来获取缓存目录并将文件保存到缓存。
最后,在您的路径 xml 中添加以下代码以允许提供程序获取 uri。
<cache-path name="cache_files" path="." />
添加回答
举报
0/150
提交
取消