I am developing an application: I am trying to set an audio file as a ringtone.
I saw many posts, but no one actually helped me, so I decided to ask this question.
I use this code when the Button is clicked:
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivityForResult(Intent.createChooser(intent, "Choose Sound File"), Audio);
}
in onActivityResult
I am trying to get the file path and then set the audio file as ringtone using the code below:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Audio && requestCode==RESULT_OK) {
Uri s1 = data.getData();
String s = s1.getPath();
if(s!=null){
try {
k = new File(new URI(s)); //(File k;)
} catch (URISyntaxException e) {
e.printStackTrace();
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_RINGTONE,
newUri);
}
}
}
Unfortunately this code doesn't work.
I would appreciate your answers. Sorry for my bad English.
setting audio as a ringtone is the same as setting tone in alarm heres the code:
using this code it allows your sound to be put in specific sd card folders and
then setting it as ringtone and alarmtone:
or you can follow a complete tutorial regarding this problem.
I saw many posts but anyone showed what i should actually have to do. So i decided to create this complete answer , in which i have the solution of my problem...
Here is my
MainActivity.java
which i usedLastly, its really important to add those permissions in your
AndroidManifest.xml
for example if you don't add the permission to write external storage your app will crash like mine.. xDWhat you need:
You can try my app on Google Play : BackAtel Audio Manager
Hope that helps.... my problem is now solved!! i hope that i solved your problem too :))
I had the same problem and couldn't find an answer anywhere.
This is the code i used.