简体   繁体   English

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

[英]How to save downloaded files in the cache folder of the app's memory?

I'm aware similar questions have been asked before but there really isn't a working solution.我知道以前有人问过类似的问题,但确实没有可行的解决方案。 I am downloading multiple PDF files through the DownloadManager and I want to save these files into the cache folder of the app's memory but couldn't find any way of doing that.我正在通过 DownloadManager 下载多个 PDF 文件,我想将这些文件保存到应用程序内存的缓存文件夹中,但找不到任何方法。

This is my code:这是我的代码:

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);
                }
            }
        });
    }
}

From the documentation文档

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 built-in download manager can't access your app internal storage, so you can only save your files into an external storage directory just by calling setDestinationInExternalFilesDir or setDestinationInExternalPublicDir . Android 内置下载管理器无法访问您的应用程序内部存储,因此您只能通过调用setDestinationInExternalFilesDirsetDestinationInExternalPublicDir将文件保存到外部存储目录中。

But if you want to save them in your internal storage, just register a receive and then copy files from the external to internal storage.但是如果你想把它们保存在你的内部存储中,只需注册一个接收,然后将文件从外部存储复制到内部存储。

you can set the local destination for the downloaded file using.您可以使用设置下载文件的本地目的地。

request.setDestinationUri(Uri uri) 

you need to use context.getCacheDir() to get cache directory and save your file to cache.您需要使用context.getCacheDir()来获取缓存目录并将文件保存到缓存。

Finally, in your paths xml add below code to allow provider to get uri.最后,在您的路径 xml 中添加以下代码以允许提供程序获取 uri。

<cache-path name="cache_files" path="."  />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM