Skip to content

Instantly share code, notes, and snippets.

@gszauer
Last active May 12, 2025 20:00
Show Gist options
  • Save gszauer/97f7646cec1a2fc8f1cabc5d250de381 to your computer and use it in GitHub Desktop.
Save gszauer/97f7646cec1a2fc8f1cabc5d250de381 to your computer and use it in GitHub Desktop.
if (!Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent); // directs user to settings screen
}
ContentValues values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DISPLAY_NAME, "Report.pdf");
values.put(MediaStore.Files.FileColumns.MIME_TYPE, "application/pdf");
values.put(MediaStore.Files.FileColumns.RELATIVE_PATH, Environment.DIRECTORY_DOCUMENTS);
// RELATIVE_PATH "Documents" directs it to /storage/emulated/0/Documents
Uri uri = getContentResolver().insert(MediaStore.Files.getContentUri("external"), values);
if (uri != null) {
try (OutputStream out = getContentResolver().openOutputStream(uri)) {
// Write your file data to the stream
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment