I am using the following code, but it doesn't seem to change the ringtone. What am I doing wrong, or is there an easier way to set an mp3 to ringtone? I have a uri that parses the location, and I call the following function. I know the uri is correct because it functions correctly with a file share method I have.
private void setRingtone(Uri uri) {
AssetFileDescriptor openAssetFileDescriptor;
((AudioManager) getActivity().getSystemService(AUDIO_SERVICE)).setRingerMode(2);
File file = new File(Environment.getExternalStorageDirectory() + "/appkeeda", mp3s[position]);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
ContentResolver contentResolver = getActivity().getContentResolver();
try {
openAssetFileDescriptor = contentResolver.openAssetFileDescriptor(uri, "r");
} catch (FileNotFoundException e2) {
openAssetFileDescriptor = null;
}
try {
byte[] bArr = new byte[1024];
FileInputStream createInputStream = openAssetFileDescriptor.createInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
for (int read = createInputStream.read(bArr); read != -1; read = createInputStream.read(bArr)) {
fileOutputStream.write(bArr, 0, read);
}
fileOutputStream.close();
} catch (IOException e3) {
e3.printStackTrace();
}
ContentValues contentValues = new ContentValues();
contentValues.put("_data", file.getAbsolutePath());
contentValues.put("title", "nkDroid ringtone");
contentValues.put("mime_type", "audio/mp3");
contentValues.put("_size", Long.valueOf(file.length()));
contentValues.put("artist", Integer.valueOf(R.string.app_name));
contentValues.put("is_ringtone", Boolean.valueOf(true));
contentValues.put("is_notification", Boolean.valueOf(false));
contentValues.put("is_alarm", Boolean.valueOf(false));
contentValues.put("is_music", Boolean.valueOf(false));
try {
//Toast.makeText(this, new StringBuilder().append("Ringtone set successfully"), Toast.LENGTH_LONG).show();
RingtoneManager.setActualDefaultRingtoneUri(getActivity().getBaseContext(), 1, contentResolver.insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), contentValues));
} catch (Throwable th) {
//Toast.makeText(this, new StringBuilder().append("Ringtone feature is not working"), Toast.LENGTH_LONG).show();
}
}
Solution: This tutorial worked for me: http://blog.nkdroidsolutions.com/set-default-ringtone-raw-folder-programmatically-android/
Related
When I run my app on Android Pie then its work fine, I mean my code for set ringtone and alarm,notification work perfectly but when I run this app on Android 7 (nougat), then alarm and notification is set but ringtone is not. When I click set ringtone then toast message display that ringtone set successfully but when I check it on my phone it still using previous ringtone.
Code for set ringtone:
private void setRingtone() {
AssetFileDescriptor openAssetFileDescriptor;
((AudioManager) getSystemService(AUDIO_SERVICE)).setRingerMode(2);
File file = new File(Environment.getExternalStorageDirectory() + "", this.fNmae);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri parse = Uri.parse(this.fPAth);
ContentResolver contentResolver = getContentResolver();
try {
openAssetFileDescriptor = contentResolver.openAssetFileDescriptor(parse, "r");
} catch (FileNotFoundException e2) {
openAssetFileDescriptor = null;
}
try {
byte[] bArr = new byte[1024];
FileInputStream createInputStream = openAssetFileDescriptor.createInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
for (int read = createInputStream.read(bArr); read != -1; read = createInputStream.read(bArr)) {
fileOutputStream.write(bArr, 0, read);
}
fileOutputStream.close();
} catch (IOException e3) {
e3.printStackTrace();
}
ContentValues contentValues = new ContentValues();
contentValues.put("_data", file.getAbsolutePath());
contentValues.put("title", "nkDroid ringtone");
contentValues.put("mime_type", "audio/mp3");
contentValues.put("_size", Long.valueOf(file.length()));
contentValues.put("artist", Integer.valueOf(R.string.app_name));
contentValues.put("is_ringtone", Boolean.valueOf(true));
contentValues.put("is_notification", Boolean.valueOf(false));
contentValues.put("is_alarm", Boolean.valueOf(false));
contentValues.put("is_music", Boolean.valueOf(false));
try {
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, parse);
Toast.makeText(this, new StringBuilder().append("Ringtone set successfully"), Toast.LENGTH_LONG).show();
} catch (Throwable th) {
Toast.makeText(this, new StringBuilder().append("Ringtone feature is not working"), Toast.LENGTH_LONG).show();
}
}
Code for alarm:
private void setAlarm() {
AssetFileDescriptor openAssetFileDescriptor;
((AudioManager) getSystemService(AUDIO_SERVICE)).setRingerMode(2);
File file = new File(Environment.getExternalStorageDirectory() + "", this.fNmae);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri parse = Uri.parse(this.fPAth);
ContentResolver contentResolver = getContentResolver();
try {
openAssetFileDescriptor = contentResolver.openAssetFileDescriptor(parse, "r");
} catch (FileNotFoundException e2) {
openAssetFileDescriptor = null;
}
try {
byte[] bArr = new byte[1024];
FileInputStream createInputStream = openAssetFileDescriptor.createInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
for (int read = createInputStream.read(bArr); read != -1; read = createInputStream.read(bArr)) {
fileOutputStream.write(bArr, 0, read);
}
fileOutputStream.close();
} catch (IOException e3) {
e3.printStackTrace();
}
ContentValues contentValues = new ContentValues();
contentValues.put("_data", file.getAbsolutePath());
contentValues.put("title", "nkDroid ringtone");
contentValues.put("mime_type", "audio/mp3");
contentValues.put("_size", Long.valueOf(file.length()));
contentValues.put("artist", Integer.valueOf(R.string.app_name));
contentValues.put("is_ringtone", Boolean.valueOf(false));
contentValues.put("is_notification", Boolean.valueOf(false));
contentValues.put("is_alarm", Boolean.valueOf(true));
contentValues.put("is_music", Boolean.valueOf(false));
try {
Toast.makeText(this, new StringBuilder().append("Alarm set successfully"), Toast.LENGTH_LONG).show();
// RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(), RingtoneManager.TYPE_RINGTONE, contentResolver.insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), contentValues));
RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(), RingtoneManager.TYPE_ALARM, parse);
Settings.System.putString(contentResolver, Settings.System.ALARM_ALERT,
parse.toString());
} catch (Throwable th) {
Toast.makeText(this, new StringBuilder().append("Alarm feature is not working"), Toast.LENGTH_LONG).show();
}
}
Below I have the coding for hiow to set audio as your ringtone. This works fine however I wanna know how could I alter this such that it well save just the audio so that it can be played with my other songs
public boolean save1(int type) {
byte[] buffer = null;
InputStream fIn = getBaseContext().getResources().openRawResource(
R.raw.song);
int size = 0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
return false;
}
String path = Environment.getExternalStorageDirectory().getPath()
+ "/media/audio/";
String filename = "New song";
FileOutputStream save;
try {
save = new FileOutputStream(path + filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + path + filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
if (RingtoneManager.TYPE_RINGTONE == type) {
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
} else if (RingtoneManager.TYPE_NOTIFICATION == type) {
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
} else if (RingtoneManager.TYPE_ALARM == type) {
values.put(MediaStore.Audio.Media.IS_ALARM, true);
}
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath());
Uri newUri = MainActivity.this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, type,
newUri);
this.getContentResolver()
.insert(MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath()), values);
return true;
}
To move raw folder file to sdcard or phone storage below code will work:
private void CopyRAWtoPhone(int id, String path) throws IOException {
InputStream in = getResources().openRawResource(id);
FileOutputStream out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
}
where first Variable id will be your raw file id and String path will be your phone storage path.
i need to add an alarm with a preset ringtone and at max volume
but i don't understand how i can pass this info to AlarmClock....
i have the ringtone in my res/raw (inside apk) and i use this code:
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(AlarmClock.EXTRA_HOUR, oratimer);
i.putExtra(AlarmClock.EXTRA_MINUTES, minutitimer);
i.putExtra(AlarmClock.EXTRA_RINGTONE, saveSong(context, R.raw.song).toString());
i.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
context.startActivity(i);
this is the saveSong function:
public Uri saveSong(Context context, int song) {
byte[] buffer = null;
InputStream fIn = context.getResources().openRawResource(song);
int size = 0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
String path = Environment.getExternalStorageDirectory()+"/Ringtones/";
String filename = "song" + ".mp3";
boolean exists = (new File(path)).exists();
if (!exists) {
new File(path).mkdirs();
}
exists = (new File(path+filename)).exists();
if (!exists) {
FileOutputStream save;
try {
save = new FileOutputStream(path + filename);
save.write(buffer);
save.flush();
save.close();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(new File(path + filename))));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
}
return Uri.fromFile(new File(path+filename));
}
but the alarm is made with the default ringtone at the default volume (like 70%?)
any hint for solve this?
tnx
I Think this would work for you
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
When I try to open a file just after inserting it using ContentResolver on Android API Level 18 it throws the exception FileNotFoundException.
If I try to run the same code on API Level 17 it works fine.
String fileName = DateFormat.format("yyyyMMdd_hhmmss", new Date()).toString();
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, fileName);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
ContentResolver contentResolver = getContentResolver();
Uri uri = contentResolver.insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = contentResolver.openOutputStream(uri);
image.compress(Bitmap.CompressFormat.JPEG, 90, outstream);
outstream.close();
return uri;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I tried using Images.Media.insertImage() but it didn't work too. Looking source code of this method I realized that code looks much like mine.
Unfortunately using ContentResolver is not working as expected. So I decided to use other approach.
try {
String fileName = DateFormat.format("yyyyMMdd_hhmmss", new Date()).toString();
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
dir.mkdirs();
File file = new File(dir, fileName + ".jpg");
OutputStream out = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
MediaScannerConnection.scanFile(this,
new String[] { file.toString() },
null,
null);
} catch (IOException e) {
e.printStackTrace();
}
I am trying to make my Android app save a mp3 from the app to the sd card so it may be of use later, but it is not saving the file anywhere in the phone. In my AndroidManifest file, I have a permission to write external storage.
Here is the code I have thus far:
public void onClick(View v) {
int ressound = R.raw.hodor1;
saveas(ressound);
}
public boolean saveas(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
//1st part
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
Log.e(TAG, "IOException first part");
return false;
}
String soundname = "hodor1";
String filename = soundname +".mp3";
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
File fullPath = new File(path, filename);
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
//second part
FileOutputStream save;
try {
save = new FileOutputStream(fullPath);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
Log.e(TAG, "FileNotFoundException in second part");
return false;
} catch (IOException e) {
Log.e(TAG, "IOException in second part");
return false;
}
//not working
//sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, soundname);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Elvis");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri newUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
// set as ringtone
//savetype = RingtoneManager.TYPE_RINGTONE;
//RingtoneManager.setActualDefaultRingtoneUri(this, savetype, newUri);
return true;
}
Commonsware answered my post the most accurately. Environment.getExternalStoragePublicDirectory() should be used along with the proper programming techniques like Logging and using the two parameter File constructor.