2

I want from my app to download mp3 file from the cloud and then set the downloaded audio file as ringtone. I tried tons of solutions through stackoverflow and spent tons of hours searching in google but without any good results.

Update:

I have fixed my issue & put my solution in the answer below. If you face my issue, I hope this code help you fix it.

Thank you

  • 1
    The answers to this question will probably help: https://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity – nukeforum Apr 05 '18 at 15:45
  • 1
    Here's another source that more directly deals with your situation: http://blog.nkdroidsolutions.com/how-to-set-ringtone-programmatically-in-android-from-sd-card/ Honestly though, you should just use Google. This is a solved problem. – nukeforum Apr 05 '18 at 15:59
  • I tried the code in this answer, it makes a ringtone with that name but it didn't work. I finally have my solution. Thanks for help – Ali Mansour Apr 05 '18 at 21:08

1 Answers1

1

After hard work of trying to fix my issue, trying more solutions and combine some of them together. finally I fixed it & there is my edited final code:

    public void setAsRingtone(String url, String title) {
try {
    String path = Environment.getExternalStorageDirectory() + "/ringtones/";
    String fileName = title + ".mp3";
    File ringtone = new File(path, fileName);
    if (!ringtone.exists()) {
        // Download Ringtone from the storage
        Toast.makeText(com.tibadev.ahlyringtones.activities.MainActivity.this, "برجاء الانتظار لحين تحميل النغمة", Toast.LENGTH_SHORT).show();
        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
        request.setAllowedOverRoaming(false);
        request.setTitle(title);
        request.setVisibleInDownloadsUi(true);
        File download = new File(Environment.getExternalStorageDirectory() + "/ringtones/", title + ".mp3");
        Uri parse = Uri.fromFile(download);
        request.setDestinationUri(parse);
        assert downloadManager != null;
        downloadManager.enqueue(request);
    }
    // Set mp3 file as ringtone
    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, ringtone.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, title);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put("_size", ringtone.length());
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtone.getAbsolutePath());
    getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtone.getAbsolutePath() + "\"", null);
    Uri newUri = com.tibadev.ahlyringtones.activities.MainActivity.this.getContentResolver().insert(uri, values);
    try {
        RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(), RingtoneManager.TYPE_RINGTONE, newUri);
    } catch (Exception e) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
                        .setData(Uri.parse("package:" + getPackageName()))
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

}

  • I had the same problem. Nice work adding the check for Marshmallow and the intent to change write settings (which doesn't work through permissions). However, I get the impression this is NOT the way android wants us to do this anymore. Perhaps there is a way to use Storage Access Frameworks? – Jeff Padgett Feb 27 '19 at 04:09