• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problems writing to SD card for a ringtone

Th3sandm4n

Newbie
Dec 31, 2009
33
2
I'm trying to set the default ringtone. I followed the code here: how to set ring tone :: anddev.org - Android Development Community | Android Tutorials
Turns out I still don't have real "write" privileges because root.canWrite() == false and no directory is being written on the sd card (this is on my phone I'm testing with) but on the emulator it works fine (until I reset the emulator, then I lose it, but that is a less important matter).


In my Manifest.xml right after </application> (I've moved it inside as well, still the same problem):
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

CODE:
/**
* Creates a ringtone in the tmp directory
* @param resourceId ID of the sound file you are using
* @param resourceName Name of the resource, this will be used for the filename
* @return A File of the mp3 file
*/
private File createTempRingtone(int resourceId, String resourceName) {
// root of the sd card
File root = Environment.getExternalStorageDirectory();
if(!root.canWrite()) {
Log.e("Create Ring Tone", "Can not write in root dir "+root.getAbsolutePath());
}
String state = Environment.getExternalStorageState();
/*if (!android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
// no sd card mounted
Log.e("Creating Temp Ring Tone", "No SD card inserted.");
return null;
}*/ //TODO debugging on phone this happens for some reason
String ringtonesPath = root.getAbsolutePath() + File.separator + "ringtoness" + File.separator;
String ringToneFileName = resourceName + ".mp3";
try {
InputStream is = getResources().openRawResource(resourceId);
File ringToneRoot = new File(ringtonesPath);
// create the dir if we need to
if(!ringToneRoot.exists()) {
ringToneRoot.mkdir();
}
if(!ringToneRoot.exists()) {
Log.e("Making Dir", "Didn't create the directory!>!?!");
}
// create the ring tone if we need to
File ringTone = new File(ringtonesPath+ringToneFileName);
if(!ringTone.exists()) {
ringTone.createNewFile();
}
// write to the file
OutputStream out = new FileOutputStream(ringTone);
byte buf[]=new byte[1024];
int len;
while((len=is.read(buf))>0)
out.write(buf,0,len);
out.close();
is.close();
return ringTone;
} catch (Exception e) {
//something happnened
Log.e("Creating Temp Ring Tone", "Something happened when creating the ringtone: "+e.getMessage());
return null;
}
}

Thanks if you have any insight :)
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones