Starting from the answer to this question
Write to Download Folder like Chrome
I am trying to write a file to the download directory. This is the code I use.
File downloadedFile = new File(getContext().getFilesDir(), FILE_NAME);
ContentResolver contentResolver = getContext().getContentResolver();
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, downloadedFile.getName());
contentValues.put(MediaStore.MediaColumns.MIME_TYPE,
contentResolver.getType(Uri.fromFile(downloadedFile)));
contentValues.put(MediaStore.MediaColumns.SIZE, downloadedFile.length());
uri = contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
} else {
//omitted
}
try(
OutputStream outputStream = contentResolver.openOutputStream(uri, "w");
BufferedInputStream bufferedInputStream
= new BufferedInputStream(
new FileInputStream(downloadedFile.getAbsoluteFile()))
) {
byte[] buff = new byte[1024];
int len;
while ((len = bufferedInputStream.read(buff)) > 0)
outputStream.write(buff, 0, len);
} catch (IOException e) {
//omitted
}
On android Q it works perfectly. On android R it says it has completed but in fact it is not in the folder. Where am I wrong? Thank you