Reputation: 21
I Need a download a audio files from server its working still 29 , and i changed the code for 30 above am facing this issue. I checked here but not found the answer.
Here is my code snippets
File destination = new File(getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + File.separator, urltoDownload.getName());
final String relativeLocation = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + File.separator + urltoDownload.getName();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Media.TITLE, urltoDownload.getName());
values.put(MediaStore.Audio.Media.DISPLAY_NAME, urltoDownload.getName());
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
values.put(MediaStore.Audio.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Audio.Media.RELATIVE_PATH, "Audio/" + relativeLocation);
Uri uri = getApplicationContext().getContentResolver().insert(MediaStore.Files.getContentUri("external"), values);
ParcelFileDescriptor descriptor = getApplicationContext().getContentResolver().openFileDescriptor(uri,"w"); //"w" specify's write mode
FileDescriptor fileDescriptor = descriptor.getFileDescriptor();
InputStream dataInputStream = getApplicationContext().openFileInput(destination.getPath());
OutputStream output = new FileOutputStream(fileDescriptor);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = dataInputStream.read(buf)) > 0)
{
output.write(buf, 0, bytesRead);
}
dataInputStream.close();
output.close();
Upvotes: 0
Views: 17263
Reputation: 9292
Adapt .RELATIVE_PATH or do not use MediaStore.Files.getContentUri("external"). Do not take the .Files. one. There are better suited.
Upvotes: 0