0

I am using this code to download the file and I want to save the file into the another app's private data directory how I can do that?

private void initiateFileDownload(String fileUrl) {
   DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl));
   request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
   request.setTitle("File Download");
   request.setDescription("Downloading file...");

   String fileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1);
   request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);

   DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
   downloadManager.enqueue(request);
 }
General Grievance
  • 4,555
  • 31
  • 31
  • 45

2 Answers2

0

As far as i know Android prevents you from doing exactly that for security reasons, so unless your device is rooted i can't imagine any way for you to archieve this.

USR
  • 1
  • 2
0

You need some permissions to write to various directories. Your app can have certain permissions, see for example this post where sdcard is considered.

It is unusual for an application to have permissions to write to other applications' directories.

Unless you are able to give your application very excessive permissions (rooted device?), I think you will need to find some other way to communicate with the other application.

May be you want just to open the file by the other app? May something like this ?

As for Environment.DIRECTORY_DOWNLOADS, this post notes you need android.permission.WRITE_EXTERNAL_STORAGE

This link explains (or un-explains) how the other application can give you permissions to certain directory or file. This would be possible if you are the author of the other application.

minorChaos
  • 101
  • 7
  • [Sharing files](https://developer.android.com/training/secure-file-sharing) – minorChaos Aug 16 '23 at 17:39
  • For a reference i download an app BGM GFX Tool which ask for a permision of the data directory of another app like this [link](https://ibb.co/qDJ2D55) – Rohya.Gadakh Aug 17 '23 at 05:42
  • This [link](https://stackoverflow.com/questions/70267192/saf-with-initial-path-prevent-changing-initial-path-in-manager) contains similar picture. I searched for `Android select folder`. Recall that there will be issue about permissions. Without the dialog, application has very limited permissions. With the dialogue, this gets extended by where *user* is permitted to save files. Does it let you go where you need ? Does ACTION_OPEN_DOCUMENT_TREE help you? (Sdk 21). There were ways before, also, [link](https://stackoverflow.com/questions/8587325/how-to-select-folder-in-android) – minorChaos Aug 17 '23 at 23:43
  • . ..[There were ways before, also] ... ... and [another link](https://stackoverflow.com/questions/31937456/how-to-browse-folder-in-android-and-get-the-path-of-folder-selected) ... and [link](https://stackoverflow.com/questions/8587325/how-to-select-folder-in-android) – minorChaos Aug 17 '23 at 23:51