[Samsung Galaxy S7 Edge Questions and Answers] Cooper.G:
Hi guys, could anyone help me with this?

I am trying to develop an application that could automatic change the ringtone for incoming calls, because all the existed applications which has this feature could not works well on my Galaxy S7 Edge,

but then I found out is't not that easy to change ringtone on Galaxy S7 Edge.

I am tried with these code:

File sdFile = new File("/mnt/storage/26D9-150C/test.mp3");

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA, sdFile.getAbsolutePath());

values.put(MediaStore.MediaColumns.TITLE, sdFile.getName());

values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");

values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);

values.put(MediaStore.Audio.Media.IS_ALARM, false);

values.put(MediaStore.Audio.Media.IS_MUSIC, false);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdFile.getAbsolutePath());

getContentResolver().delete(uri ,null ,null);//if not delete, maybe it will cause some error.

Uri newUri = this.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);

These code works fine on other devices, also on the Galaxy S7 Edge, you can see the new ringtone with code below, the ringtone changed into the target file, but when you received a phone call it's not the the ringtone that I set ! It's still the old ringtone before I run that code above. What's the problem ????? And I checked the ringtone from "Setting-Sound and vibrate-ringtone-incoming ringtone" it's never changed!!!

And I have granted the WRITE_SETTINGS permission already.

private Uri getSystemDefaultRingtoneUri() {

return RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);//RingtoneManager.TYPE_ALL

}

private String getRealPath(Uri fileUrl) {

String fileName = null;

Uri filePathUri = fileUrl;

if (fileUrl != null) {

if (fileUrl.getScheme().toString().compareTo("content") == 0) {

Cursor cursor = mContext.getContentResolver().query(fileUrl, null, null, null, null);

if (cursor != null && cursor.moveToFirst()) {

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

fileName = cursor.getString(column_index);

if (!fileName.startsWith("/mnt")) {

fileName = "/mnt" + fileName;

}

cursor.close();

}

} else if (fileUrl.getScheme().compareTo("file") == 0) {

fileName = filePathUri.toString().replace("file://", "");

if (!fileName.startsWith("/mnt")) {

fileName += "/mnt";

}

}

}

return fileName;

}

Then I searched everywhere, and find out that it seems samsung changed the system, the TYPE_RINGTONE will not effect on samsung anymore, is it true ? How could fix this problem ?

Why samsung change it like this ? It's really ****ty for developers!

Could anyone help me ? Thank you!

And here is all the code:

public class ActivityMain extends AppCompatActivity {

private static final boolean d = true;

private Context mContext;

[MENTION=439709]override[/MENTION]

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mContext = this;

initView();

checkMyPermission(false);

}

private boolean checkMyPermission(boolean isSecondTimeCheck) {

String dialogTitle = "need WRITE_SETTINGS permission";

if (isSecondTimeCheck) dialogTitle = "you deny the permission";

String btnTitle = "ok", if (isSecondTimeCheck) btnTitle = "try again";

if (Build.VERSION.SDK_INT >= 23) {

if (!Settings.System.canWrite(this)) {

new AlertDialog.Builder(mContext).setMessage(dialogTitle).setPositiveButton(btnTitle, new DialogInterface.OnClickListener() {
[MENTION=439709]override[/MENTION]

public void onClick(DialogInterface dialog, int which) {

Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));

startActivityForResult(intent, REQUEST_CODE_PERMISSION);

}

}).setNegativeButton("cancel", null).create().show();

return false;

}

return true;

} else {

int hasWriteContactsPermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_SETTINGS);

if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {

new AlertDialog.Builder(mContext).setMessage(dialogTitle).setPositiveButton(btnTitle, new DialogInterface.OnClickListener() {
[MENTION=439709]override[/MENTION]

public void onClick(DialogInterface dialog, int which) {

Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));

startActivityForResult(intent, REQUEST_CODE_PERMISSION);

}

}).setNegativeButton("cancel", null).create().show();

return false;

} else {

return true;

}

}

}

private void initView() {

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {
[MENTION=439709]override[/MENTION]

public void onClick(View view) {

//TODO I added the event here, it's more easier......this is the automatic method

// Log.d("tag", "original ringtone before set new ringtone" + getRealPath(getSystemDefaultRingtoneUri()));

// setMyRingtone("/mnt/storage/26D9-150C/0_MyFiles/8.Others/New_Eng_Rings_15.09.03/Blame it on me - akon - A.mp3");

// //setMyRingtone("/mnt/storage/26D9-150C/0_MyFiles/8.Others/New_Eng_Rings_15.09.03/Birthmark - akon - A.mp3");

//TODO here is the 2nd method, manual setting also not work....

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);

intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);

intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Choose the new ringtone");

startActivityForResult(intent, REQUEST_CODE_CHOOSE_RINGTONE_BY_USER);

}

});

}

private static final int REQUEST_CODE_PERMISSION = 1;

private static final int REQUEST_CODE_CHOOSE_RINGTONE_BY_USER = 2;

private void setMyRingtone(String path) {

File sdFile = new File(path);

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA, sdFile.getAbsolutePath());

values.put(MediaStore.MediaColumns.TITLE, sdFile.getName());

values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");

values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);

values.put(MediaStore.Audio.Media.IS_ALARM, false);

values.put(MediaStore.Audio.Media.IS_MUSIC, false);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdFile.getAbsolutePath());

getContentResolver().delete(uri, null, null);

Uri newUri = this.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);

Log.d("tag", "new ringtone has been set:" + getRealPath(getSystemDefaultRingtoneUri()));//you can see that the new ringtone has been set success!

}

private Uri getSystemDefaultRingtoneUri() {

return RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);//RingtoneManager.TYPE_ALL

}

private String getRealPath(Uri fileUrl) {

String fileName = null;

Uri filePathUri = fileUrl;

if (fileUrl != null) {

if (fileUrl.getScheme().toString().compareTo("content") == 0) {

Cursor cursor = mContext.getContentResolver().query(fileUrl, null, null, null, null);

if (cursor != null && cursor.moveToFirst()) {

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

fileName = cursor.getString(column_index);

if (!fileName.startsWith("/mnt")) {

fileName = "/mnt" + fileName;

}

cursor.close();

}

} else if (fileUrl.getScheme().compareTo("file") == 0) {

fileName = filePathUri.toString().replace("file://", "");

if (!fileName.startsWith("/mnt")) {

fileName += "/mnt";

}

}

}

return fileName;

}

/* 当设置铃声之后的回调函数 */
[MENTION=439709]override[/MENTION]

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {

case REQUEST_CODE_PERMISSION:

checkMyPermission(true);

break;

case REQUEST_CODE_CHOOSE_RINGTONE_BY_USER:

if (resultCode == RESULT_OK) {

try {

Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

if (pickedUri != null) {

Log.d("tag", "new ringtone will be set into:" + getRealPath(pickedUri));

RingtoneManager.setActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_RINGTONE, pickedUri);

Log.d("tag", "new ringtone has been set into:" + getRealPath(getSystemDefaultRingtoneUri()));

}

} catch (Exception e) {

if (d) e.printStackTrace();

}

}

break;

}

super.onActivityResult(requestCode, resultCode, data);

}

}

Please help, thank you.