3

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.

Ramesh R
  • 7,009
  • 4
  • 25
  • 38

3 Answers3

2

You cannot use DownloadManager to download directly to your app's portion of internal storage. You will need to use OkHttp or some other in-process HTTP client API.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You can use this:

request.setDestinationUri(Uri.fromFile(new File(context.getCacheDir(),"filename.txt")));
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
Parth Suthar
  • 123
  • 4
0

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

touhid udoy
  • 4,005
  • 2
  • 18
  • 31