Downloading PDF files in internal storage

Viewed 5281

I'm making the task of Android.

I download the file from URL and save in internal storage/directory of android phone? my code is written below but this is external storage code I want internal storage code.

 public void onClick(View v) {
                    Toast.makeText(Computer.this,"Please Wait until the file is download",Toast.LENGTH_LONG).show();
                    downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    Uri uri = Uri.parse("url");
                    DownloadManager.Request request = new DownloadManager.Request(uri);
                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                    request.setAllowedOverRoaming(false);
                    request.setTitle("" + "filename" + ".pdf");
                    request.setVisibleInDownloadsUi(true);
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    Long reference = downloadManager.enqueue(request);
                    request.setDestinationInExternalFilesDir(Environment.DIRECTORY_DOWNLOADS, "/"+ "filename");
                    refid = downloadManager.enqueue(request);
                    Log.e("OUT", "" + refid);

That is external storage code but I want to save in internal storage of Android phone.

3 Answers

You can use this:

request.setDestinationUri(Uri.fromFile(new File(context.getCacheDir(),"filename.txt")));

You can simple just delete this line, request.setDestinationInExternalFilesDir()

By default, downloads are saved to a generated filename in the shared download cache

From Docs

Related