How to Store Files Generated from App in "Downloads" Folder of Android

How to store files generated from app in Downloads folder of Android?

Use this to get the directory:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

And don't forget to set this permission in your manifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Create CSV or TXT file in app and save it to 'download' folder - Android

Save to to publicDir(Downloads folder) you first need permission.WRITE_EXTERNAL_STORAGE
check docs

Note this won't work without permmissions

   private void saveData(){

String csv_data = "";/// your csv data as string;
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

//if you want to create a sub-dir
root = new File(root, "SubDir");
root.mkdir();

// select the name for your file
root = new File(root , "my_csv.csv");

try {
FileOutputStream fout = new FileOutputStream(root);
fout.write(csv_data.getBytes());

fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();

boolean bool = false;
try {
// try to create the file
bool = root.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}

if (bool){
// call the method again
saveData()
}else {
throw new IllegalStateException("Failed to create image file");
}
} catch (IOException e) {
e.printStackTrace();
}
}

Access files of another App inside the Download folder - Android 10+

If the second app has 'all files access' with MANAGE_EXTERNAL_STORAGE it can also list the files of the first app.

Otherwise you can let the user of the second app pick those files with ACTION_OPEN_DOCUMENT of Storage Access Framework.

You better store your files in a sub directory as then second app can pick complete directory with ACTION_OPEN_DOCUMENT_TREE and list all the files.

How do i download files to the local downloads folder?

According to documentation there are 2 types of external storage

  • Public files: Files that should be freely available to other apps and to the user. When the user uninstalls your app, these files should remain available to the user. For example, photos captured by your app or other downloaded files should be saved as public files.
  • Private files: Files that rightfully belong to your app and will be deleted when the user uninstalls your app. Although these files are technically accessible by the user and other apps because they are on the external storage, they don't provide value to the user outside of your app.

In your code, calling DownloadManager.Request.setDestinationInExternalFilesDir() is equivalent to calling Context.getExternalFilesDir() which will get private file directory.

If you want to save downloaded files to Download directory, use DownloadManager.Request.setDestinationInExternalPublicDir()

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "x.mp3");
// call allowScanningByMediaScanner() to allow media scanner to discover your file
request.allowScanningByMediaScanner();


Related Topics



Leave a reply



Submit