Skip to content

Commit

Permalink
download to a specific directory
Browse files Browse the repository at this point in the history
resolves #745
  • Loading branch information
KiruPoruno committed Sep 4, 2024
1 parent 2c9e92a commit 9d4d509
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,15 @@ private OutputStream destinationStreamForFile(Attachment att) throws IOException
ContentValues values=new ContentValues();
// values.put(MediaStore.Downloads.DOWNLOAD_URI, att.url);
values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + "/" + activity.getApplicationInfo().loadLabel(activity.getPackageManager()));
String mime=mimeTypeForFileName(fileName);
if(mime!=null)
values.put(MediaStore.MediaColumns.MIME_TYPE, mime);
ContentResolver cr=activity.getContentResolver();
Uri itemUri=cr.insert(MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), values);
return cr.openOutputStream(itemUri);
}else{
return new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName));
return new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), activity.getApplicationInfo().loadLabel(activity.getPackageManager()) + "/" + fileName));
}
}

Expand All @@ -568,7 +568,7 @@ private void doSaveCurrentFile(){
});
if(Build.VERSION.SDK_INT<29){
String fileName=Uri.parse(att.url).getLastPathSegment();
File dstFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
File dstFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), activity.getApplicationInfo().loadLabel(activity.getPackageManager()) + "/" + fileName);
MediaScannerConnection.scanFile(activity, new String[]{dstFile.getAbsolutePath()}, new String[]{mimeTypeForFileName(fileName)}, null);
}
}catch(IOException x){
Expand Down Expand Up @@ -596,7 +596,7 @@ private void saveViaDownloadManager(Attachment att){
DownloadManager.Request req=new DownloadManager.Request(uri);
req.allowScanningByMediaScanner();
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, activity.getApplicationInfo().loadLabel(activity.getPackageManager()) + "/" + uri.getLastPathSegment());
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.TIRAMISU)
activity.registerReceiver(downloadCompletedReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), Context.RECEIVER_EXPORTED);
else
Expand Down Expand Up @@ -1108,4 +1108,4 @@ public void onCompletion(MediaPlayer mp){
windowView.removeCallbacks(uiAutoHider);
}
}
}
}

0 comments on commit 9d4d509

Please sign in to comment.