androidandroid-download-managerdownload-managerinternal-storageandroid-internal-storage

W/DownloadManager: Aborting request for download 17: Failed to create target file /storage/emulated/0/Ringtone/Fav_Ringtone.mp3


I am trying to download my mp3 file and want to save it to internal storage at a specific location.

But every time my download is failing.

Logcat error message

01-01 08:36:09.295 154-748/android.process.media W/DownloadManager: Aborting request for download 
17: Failed to create target file /storage/emulated/0/Ringtone/Fav_Ringtone.mp3

Download Method

   public void Save_mp3_internal_Storage(Uri uri,Context context){

        String destination = Environment.getExternalStorageDirectory()+"/Ringtone/";

        Delete_mp3_internal_Storage(new File(destination,"Fav_Ringtone.mp3"));

        DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

        DownloadManager.Request request = new DownloadManager.Request(uri)
                .setDestinationUri(Uri.fromFile(new File(destination,"Fav_Ringtone.mp3")))
                .setTitle("Ringtone Downloading")
                .setNotificationVisibility(1);
                 manager.enqueue(request);
    }

    public void Delete_mp3_internal_Storage(File file){
        if (file.exists()){
            file.delete();
        }
    }

I think it is an emulator fault. because most of the time my emulator fails to download the files(SmartGaga)


Solution

  • change your code to this

        public void Save_mp3_internal_Storage(Uri uri, Context context){
    
            Delete_mp3_internal_Storage(context);
    
            DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    
            DownloadManager.Request request = new DownloadManager.Request(uri);
                    request.setDestinationInExternalFilesDir(context,Environment.DIRECTORY_ALARMS,"Fav_Ringtone.mp3")
                    .setTitle("Ringtone Downloading")
                    .setNotificationVisibility(1);
                     manager.enqueue(request);
        }
    
        public void Delete_mp3_internal_Storage(Context context){
           File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS),"Fav_Ringtone.mp3");
           if (file.exists()){file.delete();}
        }