代码之家  ›  专栏  ›  技术社区  ›  Levent Ozbek

如何将下载的文件保存在应用程序内存的缓存文件夹中?

  •  1
  • Levent Ozbek  · 技术社区  · 7 年前

    这是我的密码:

    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 回复  |  直到 7 年前
        1
  •  3
  •   Darush    5 年前

    documentation

    内部存储器 : 应用程序。当用户卸载你的应用程序时,系统会删除你的所有应用程序 来自内部存储的应用程序文件。

    Android内置的下载管理器无法访问你的应用程序内部存储,因此你只能通过调用 setDestinationInExternalFilesDir setDestinationInExternalPublicDir .

    但是,如果您想将它们保存在内部存储器中,只需注册一个receive,然后将文件从外部存储器复制到内部存储器。

        2
  •  0
  •   karan    7 年前

    可以使用设置下载文件的本地目标。

    request.setDestinationUri(Uri uri) 
    

    context.getCacheDir() 获取缓存目录并将文件保存到缓存。

    最后,在xml路径中添加以下代码,以允许提供者获取uri。

    <cache-path name="cache_files" path="."  />
    
    推荐文章