简体   繁体   中英

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.

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 .

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.

Finally, in your paths xml add below code to allow provider to get uri.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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