1. Check out our companion app, Forums for Android! Download from Google Play

Problems writing to SD card for a ringtone

Discussion in 'Application Development' started by Th3sandm4n, Apr 19, 2010.

  1. Th3sandm4n

    Th3sandm4n Active Member
    Thread Starter

    Dec 31, 2009
    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" />

    * 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()) {
    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()) {
    // write to the file
    OutputStream out = new FileOutputStream(ringTone);
    byte buf[]=new byte[1024];
    int len;
    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 :)


Share This Page